空 对比 undefined
一句话解释
undefined 平均值 变量 有 是 declared 但 不 assigned 值; 空 是 intentional absence 的 值.
用大白话说
undefined and null 两者都 represent '否值', 但它们平均值不同 things. undefined 是 JavaScript's 方式的 saying "这个变量 exists 但有 否值 yet" — 它是 assigned automatically 通过 language. null is a value you assign intentionally to say "这个是 deliberately 空."
undefined appears when: a variable is declared but not assigned, a function has no return statement, a function parameter is not provided, or an object property does not exist. 空 is only present when you explicitly set it.
They are loosely equal (空 == undefined is true) but strictly different (空 === undefined is false). typeof undefined is "undefined", while typeof 空 is "对象" (a historical bug in JavaScript).
示例
let a;
console.log(a); // undefined
const b = null;
console.log(b); // null
console.log(a == b); // true (loose)
console.log(a === b); // false (strict)undefined 是 automatic; 空 是 intentional. 它们 是 loosely equal 但 strictly 不同.
常见混淆
容易混淆的对象: 空
关键区别: undefined 是 assigned 通过 JavaScript 当 否 值 是 given; 空 是 assigned 通过 programmer 到 indicate intentional emptiness.
相关术语
相关资源
相关课程
- 数据 类型
学习 关于 空 and undefined.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。