当前位置:网站首页>[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 || '';
边栏推荐
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- vant tabbar遮挡内容的解决方式
- 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
- The database of the server is not connected to 200310060 "unknown error" [the service is up, the firewall is off, the port is on, and the netlent port is not connected]
- 16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
- 一些认知的思考
- 2020-2022 two-year anniversary of creation
- Single merchant v4.4 has the same original intention and strength!
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
猜你喜欢

sql中set标签的使用

Today's sleep quality record 79 points

极坐标扇图使用场景与功能详解

Single merchant v4.4 has the same original intention and strength!

Why should we learn mathematical modeling?

单商户 V4.4,初心未变,实力依旧!

Which keywords will conflict with the abstract keyword

五种常见的咨询公司谈判策略以及如何维护自己的利益

Migrate /home partition

Batch update in the project
随机推荐
极坐标扇图使用场景与功能详解
ES6 drill down - Async functions and symbol types
Modify PyUnit_ Time makes it support the time text of 'xx~xx months'
Mistakes made when writing unit tests
EDI许可证和ICP经营性证有什么区别
obj解析为集合
开发中Boolean类型使用遇到的坑
抽象类中子类与父类
abstract关键字和哪些关键字会发生冲突呢
[graduation season] as a sophomore majoring in planning, I have something to say
Query the latest record in SQL
10分钟帮你搞定Zabbix监控平台告警推送到钉钉群
sql中set标签的使用
异常com.alibaba.fastjson.JSONException: not match : - =
程序员如何提升自己的格局?
Quick completion guide for manipulator (IX): forward kinematics analysis
Mongodb getting started Tutorial Part 04 mongodb client
Cs231n notes (medium) -- applicable to 0 Foundation
Use of RLOCK lock
写单元测试的时候犯的错