分享学习记录
互联网技术知识

记录下js的优秀写法

很多js的优秀写法能简化我们的代码数量,而且能有效的执行,写出优秀的代码是一个程序员的追求。

// 多条件判断的精简写法 
const options ['si','xue','tang'];
if (options.includes(value)){
  return ;
}
// 如果条件不满足,则直接返回
function handleFun(event)
  if (!event || !event.target){
    return
  }
}
// es6新增语法,可以避免null值无属性的报错
const value reponse?.data?.text // 链式运算符,无则返回undefined
// 取数组最后一位
const value = arr.at(-1)
// 三目运算符赋值
const value = a ? b : c
// 数组去重
const value = (arr)=> [... new Set(arr)]
//去除数组空项
let arr=[1,2,,4,5];
arr.flat(arr)
const a = 3;
const c = null

// a||c,输出第一个不为Falsy的值
// falsy有false、0、-0、0n、""、null、undefined 和 NaN
console.log(a || c);
// Expected output: 3

// a&&c,输出第一个不为Truthy的值
// 除 false、0、-0、0n、""、null、undefined 和 NaN 以外的皆为Truthy
console.log(a && c);
// Expected output: null
赞(0)
文章名称:《记录下js的优秀写法》
文章链接:https://www.bailuze.com/8285.html
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站专注于百度、搜狗、360、谷歌、bing等常见搜索引擎的优化,关键词排名的提高,诚意咨询邮箱526009505@qq.com
分享到