当前位置:网站首页>[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 || '';
边栏推荐
- Cartoon: what is distributed transaction?
- 10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
- 【网易云信】超分辨率技术在实时音视频领域的研究与实践
- 用键盘输入一条命令
- 一些认知的思考
- The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry
- Use of RLOCK lock
- 阿掌的怀念
- 漫画:什么是蓝绿部署?
- 21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
猜你喜欢
新春限定丨“牛年忘烦”礼包等你来领~
Pits encountered in the use of boolean type in development
List de duplication and count the number
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
Win11如何给应用换图标?Win11给应用换图标的方法
后台系统发送验证码功能
[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video
Which keywords will conflict with the abstract keyword
Cs231n notes (top) - applicable to 0 Foundation
随机推荐
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
国泰君安网上开户安全吗
单商户 V4.4,初心未变,实力依旧!
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
Some cognitive thinking
EDI许可证和ICP经营性证有什么区别
写单元测试的时候犯的错
Cs231n notes (top) - applicable to 0 Foundation
怎样在电脑上设置路由器的WiFi密码
Cartoon: what is blue-green deployment?
Convert obj set to entity set
ES6深入—ES6 Class 类
五种常见的咨询公司谈判策略以及如何维护自己的利益
[Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video
后台系统发送验证码功能
Today's sleep quality record 79 points
Quelques réflexions cognitives
list去重并统计个数
事务回滚异常