[TypeScript ] Using the Null Coalescing operator with TypeScript 3.7
2024-08-22 21:29:05
The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set default values in TypeScript 3.7 to prevent expected bugs in your code.
const val = ;
const correct = (val !== undefined && val !== null) ? val : 0.5;
// const incorrect = val || 0.5; // || check all the falsy value
const incorrect = val ?? 0.5; // ?? check only null and undefined
console.log({ correct, incorrect });
最新文章
- Python成长笔记 - 基础篇 (七)python面向对象
- 开发工程师面试的秘密( 整理自 Export C Programming )
- CSS z-index 属性的使用方法和层级树的概念
- Java-->;Gson序列化及反序列化
- ios block和函数的区别
- 关于eclipse中删除多余的工作空间记录
- 【java】利用异常机制,往前台写错误信息
- 修改css
- Spring MVC 关于分页的简单实现
- 【2017-03-20】HTML基础知识、文字标记、图片标记、空格换行、表格、表格嵌套及布局、超链接
- grub2与grub区别
- LOJ #6060. 「2017 山东一轮集训 Day1 / SDWC2018 Day1」Set
- 解决layui table方法渲染时时间格式问题
- 开源中国/码云 README.md上传图片的爬坑记录
- Spark SQL例子
- C#网络编程技术FastSocket实战项目演练
- jmeter ---模拟发送TCP/UDP/HTTP/FTP等请求包
- Supervisor 进程管理工具
- ssh-keygen生成私钥和公钥
- Objective-C:异常的处理