?操作符或可選的鍊式運算符是一個很有用的運算符,用于檢查一個值是否已經被設定,當它被設定後再繼續。
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