可选链 null
预测输出 入门
题目
这段代码打印什么?
代码
const obj = null;
console.log(obj?.prop);
提示
可选链对 null/undefined 返回 undefined。
解释
obj 为 null。obj?.prop 短路并返回 undefined 而非抛出 "Cannot read property prop of null"。
相关资源
相关课程
- 可选链
学习可选链。
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。