In JavaScript, undefined
means a variable has been declared but has not yet been assigned a value, such as:
(在JavaScript中, undefined
表示变量已被声明但尚未分配值,例如:)
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
null
is an assignment value.
(null
是赋值。)
It can be assigned to a variable as a representation of no value: (可以将其分配给变量,以表示没有值:)
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
From the preceding examples, it is clear that undefined
and null
are two distinct types: undefined
is a type itself (undefined) while null
is an object.
(从前面的示例中可以明显看出, undefined
和null
是两种不同的类型: undefined
是类型本身(未定义),而null
是对象。)
null === undefined // false
null == undefined // true
null === null // true
and
(和)
null = 'value' // ReferenceError
undefined = 'value' // 'value'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…