当前位置 : 主页 > 网页制作 > HTTP/TCP >

[TypeScript ] Using the Null Coalescing operator with TypeScript 3.7

来源:互联网 收集:自由互联 发布时间:2021-06-16
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 = 0 ; const correct = (val !== undefined val !== null ) ? val

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 = 0;
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 });
上一篇:前端基础
下一篇:请求转发和重定向
网友评论