当前位置:网站首页>[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 || '';
边栏推荐
- 降本40%!Redis多租户集群的容器化实践
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 【毕业季】作为一名大二计科在校生,我有话想说
- tf.sequence_mask函数讲解案例
- 迁移/home分区
- 英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
- obj解析为集合
- 践行自主可控3.0,真正开创中国人自己的开源事业
- 漫画:什么是分布式事务?
- Six common transaction solutions, you sing, I come on stage (no best, only better)
猜你喜欢
开发中Boolean类型使用遇到的坑
服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
Li Kou today's question -729 My schedule I
19.[STM32]HC_ SR04 ultrasonic ranging_ Timer mode (OLED display)
18.[stm32] read the ROM of DS18B20 temperature sensor and realize multi-point temperature measurement
How difficult is it to pass the certification of Intel Evo 3.0? Yilian technology tells you
单商户 V4.4,初心未变,实力依旧!
List de duplication and count the number
Flet教程之 12 Stack 重叠组建图文混合 基础入门(教程含源码)
随机推荐
Cartoon: what is distributed transaction?
Mongodb getting started Tutorial Part 04 mongodb client
国泰君安网上开户安全吗
后台系统发送验证码功能
记一次'非常诡异'的云安全组规则问题排查过程
Five common negotiation strategies of consulting companies and how to safeguard their own interests
21. [STM32] I don't understand the I2C protocol. Dig deep into the sequence diagram to help you write the underlying driver
【漏洞预警】CVE-2022-26134 Confluence 远程代码执行漏洞POC验证与修复过程
研发效能度量指标构成及效能度量方法论
obj集合转为实体集合
yarn 常用命令
StarkWare:欲构建ZK“宇宙”
效果编辑器新版上线!3D渲染、加标注、设置动画,这次一个编辑器就够了
Batch update in the project
How can programmers improve their situation?
Research and development efficiency measurement index composition and efficiency measurement methodology
Some cognitive thinking
数据湖(十四):Spark与Iceberg整合查询操作
[vulnerability warning] cve-2022-26134 conflict Remote Code Execution Vulnerability POC verification and repair process
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……