当前位置:网站首页>[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 || '';
边栏推荐
- The memory of a Zhang
- yarn 常用命令
- The difference between abstract classes and interfaces
- Cartoon: what is MapReduce?
- RLock锁的使用
- [Netease Yunxin] research and practice of super-resolution technology in the field of real-time audio and video
- 《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
- You should have your own persistence
- 五种常见的咨询公司谈判策略以及如何维护自己的利益
- 英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
猜你喜欢

Coding devsecops helps financial enterprises run out of digital acceleration

Five common negotiation strategies of consulting companies and how to safeguard their own interests

迁移/home分区
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存

Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022

Intelligent metal detector based on openharmony

怎样在电脑上设置路由器的WiFi密码

The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry

中间表是如何被消灭的?

abstract关键字和哪些关键字会发生冲突呢
随机推荐
事务回滚异常
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
vant popup+其他组件的组合使用,及避坑指南
List uses stream flow to add according to the number of certain attributes of the element
Win11如何给应用换图标?Win11给应用换图标的方法
漫画:什么是服务熔断?
Cs231n notes (top) - applicable to 0 Foundation
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
中间表是如何被消灭的?
Record the pits encountered in the raspberry pie construction environment...
Migrate /home partition
Seaborn绘制11个柱状图
Obj resolves to a set
Today's sleep quality record 79 points
Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
极坐标扇图使用场景与功能详解
Six common transaction solutions, you sing, I come on stage (no best, only better)
后台系统发送验证码功能
Convert obj set to entity set