1.可选的链
js对象属性取值,对象属性不存在时不报错
const user = {
employee: {
name: "Kapil"
}
};
user.employee?.name;
// Output: "Kapil"
user.employ?.name;
// Output: undefined
user.employ.name
// 输出: VM21616:1 Uncaught TypeError: Cannot read property 'name' of undefined
const obj={n:0,m:null,k:undefined}
const res = obj?.j||100
//100
2.双问号语法
js实现值为null或undefined时默认赋值
const foo = null ?? 'my school';
// 输出: "my school"
const baz = 0 ?? 42;
// 输出: 0
```javascript
const obj={n:0,m:null,k:undefined}
const res1 = obj.n??100
//0
const res2 = obj.m??100
//100
const res3 = obj.k??100
//100
//结合使用
const res4= obj?.settings?.n ?? 100
//100
这个 ?? 的意思是,如果 ?? 左边的值是 null 或者 undefined,那么就返回右边的值。