null、undefined、undeclared(未声明)变量有什么区别?如何检查它们?
What's the difference between a variable that is: `null`, `undefined` or undeclared? How would you go about checking for any of these states?
题目类型: 技术面试题
这是一道技术面试题,常见于澳洲IT公司面试中。
难度: easy
分类: javascript-fundamentals
标签: null, undefined, undeclared
参考答案摘要
答案 undeclared:变量从未用 var/let/const 声明过就直接使用/赋值;非严格模式可能会意外创建全局变量,严格模式下会抛 ReferenceError。检查通常需要 try/catch 或 `typeof x`(对未声明变量不会抛错)。 undefined:变量已声明但未赋值,或函数没有显式 return。检查用 `x === undefined` 或 `typeof x ==...
本题提供 STAR 原则详细解答和技术解析,登录匠人学院学习中心即可查看完整答案。