?操作符与??操作符

?操作符或可选的链式运算符是一个很有用的运算符,用于检查一个值是否已经被设置,当它被设置后再继续。

if(data && data.subdata && data.subdata.name === "cool") {
console.log("hi")
}
// Is the same as
if(data?.subdata?.name === "cool") {
console.log("hi")
}

??操作符是一个检查一条语句左值是否为空的操作符,如果为真,它将返回右边的值。

const x = null ?? 'string';
// x: "string"
const y = 12 ?? 42;
// y: 12

 

posted @ 2022-02-08 10:19  菜鸟小何  阅读(82)  评论(0编辑  收藏  举报