当前位置:网站首页>[js] 技巧 简化if 判空
[js] 技巧 简化if 判空
2022-07-05 15:49:00 【533_】
1. 简化条件表达式
// 太长的逻辑表达式
if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {
//其他逻辑
}
// 简写
if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
//其他逻辑
}
简化 if … else
// 新手的写法
let test= boolean;
if (x > 100) {
test = true;
} else {
test = false;
}
// 简写表达式
let test = (x > 10) ? true : false;
// 更直接的
let test = x > 10;
console.log(test);
判空并赋默认值
if (first !== null || first !== undefined || first !== '') {
let second = first;
}
// 简写
let second = first || '';
边栏推荐
- 五种常见的咨询公司谈判策略以及如何维护自己的利益
- CISP-PTE之SQL注入(二次注入的应用)
- Pits encountered in the use of boolean type in development
- ES6 drill down - ES6 generator function
- scratch五彩糖葫芦 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
- Convert obj set to entity set
- 开发中Boolean类型使用遇到的坑
- Cartoon: what is distributed transaction?
- Subclasses and superclasses of abstract classes
- HiEngine:可媲美本地的云原生内存数据库引擎
猜你喜欢
ES6 drill down - Async functions and symbol types
ES6 deep - ES6 class class
数据湖(十四):Spark与Iceberg整合查询操作
Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
Seaborn绘制11个柱状图
ES6 drill down - ES6 generator function
Intelligent metal detector based on openharmony
Codasip adds verify safe startup function to risc-v processor series
Convert obj set to entity set
【网易云信】超分辨率技术在实时音视频领域的研究与实践
随机推荐
abstract关键字和哪些关键字会发生冲突呢
迁移/home分区
写单元测试的时候犯的错
降本40%!Redis多租户集群的容器化实践
效果编辑器新版上线!3D渲染、加标注、设置动画,这次一个编辑器就够了
Batch update in the project
Which keywords will conflict with the abstract keyword
Using graylog alarm function to realize the regular work reminder of nail group robots
Transaction rollback exception
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
公司自用的国产API管理神器
单商户 V4.4,初心未变,实力依旧!
研发效能度量指标构成及效能度量方法论
项目中批量update
Some cognitive thinking
Dare not buy thinking
五种常见的咨询公司谈判策略以及如何维护自己的利益
[深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式
单商户 V4.4,初心未变,实力依旧!
19.[STM32]HC_ SR04 ultrasonic ranging_ Timer mode (OLED display)