我想知道JavaScript中null
和undefined
之间的区别。 javascript
因为typeof返回undefined,所以undefined是一种类型,其中null表示一个初始化器,该变量表示没有对象(Javascript中的几乎全部对象都是对象)。 java
null和undefined都用于表示缺乏某些值。 函数
var a = null;
初始化并定义。 测试
typeof(a) //object
null是JavaScript中的对象 this
Object.prototype.toString.call(a) // [object Object] var b;
b是未定义和未初始化的 lua
未定义的对象属性也是未定义的。 例如,在对象c上未定义“ x”,而且若是您尝试访问cx,它将返回未定义。 spa
一般,咱们将null分配给未定义的变量。 prototype
null是表示没有值的特殊关键字。 code
将其视为一个值,例如: 对象
undefined属性表示还没有为变量分配包含null的值。 喜欢
var foo;
已定义的空变量为数据类型undefined
null
他们两个都表明一个没有值的变量的值
与 null
并不表明没有价值的字符串 -空与字符串
喜欢
var a = ''; console.log(typeof a); // string console.log(a == null); //false console.log(a == undefined); // false
如今若是
var a; console.log(a == null); //true console.log(a == undefined); //true
但
var a; console.log(a === null); //false console.log(a === undefined); // true
因此每一个人都有本身的使用方式
undefined用它来比较变量数据类型
null使用它来清空变量的值
var a = 'javascript'; a = null ; // will change the type of variable "a" from string to object
在JavasSript中,有5种原始数据类型String,Number,Boolean,null和undefined。 我将尝试用一些简单的例子来解释
能够说咱们有一个简单的功能
function test(a) { if(a == null){ alert("a is null"); } else { alert("The value of a is " + a); } }
也在上面的函数中if(a == null)与if(!a)相同
如今,当咱们调用此函数而不传递参数a时
test(); it will alert "a is null"; test(4); it will alert "The value of a is " + 4;
也
var a; alert(typeof a);
这将给未定义; 咱们已经声明了一个变量,但没有给该变量赋任何值; 可是若是咱们写
var a = null; alert(typeof a); will give alert as object
因此null是一个对象。 以某种方式,咱们为'a'分配了null值
null和undefined是两种不一样的对象类型,它们具备如下共同点:
==
和!=
运算符认为null和undefined值彼此相等,而别无其余。 可是,类似之处到此结束。 一次,关键字null和undefined的实现方式存在根本差别。 这并不明显,可是请考虑如下示例:
var undefined = "foo"; WScript.Echo(undefined); // This will print: foo
undefined , NaN和Infinity只是预初始化的“超全局”变量的名称-它们在运行时进行初始化,而且能够被具备相同名称的常规全局或局部变量覆盖。
如今,让咱们尝试使用null进行相同的操做:
var null = "foo"; // This will cause a compile-time error WScript.Echo(null);
糟糕! null , true和false是保留关键字-编译器不容许您将它们用做变量或属性名称
另外一个区别是, undefined是原始类型,而null是对象类型(表示没有对象引用)。 考虑如下:
WScript.Echo(typeof false); // Will print: boolean WScript.Echo(typeof 0); // Will print: number WScript.Echo(typeof ""); // Will print: string WScript.Echo(typeof {}); // Will print: object WScript.Echo(typeof undefined); // Will print: undefined WScript.Echo(typeof null); // (!!!) Will print: object
此外,在数字上下文中处理null和undefined的方式也存在重要区别:
var a; // declared but uninitialized variables hold the value undefined WScript.Echo(a === undefined); // Prints: -1 var b = null; // the value null must be explicitly assigned WScript.Echo(b === null); // Prints: -1 WScript.Echo(a == b); // Prints: -1 (as expected) WScript.Echo(a >= b); // Prints: 0 (WTF!?) WScript.Echo(a >= a); // Prints: 0 (!!!???) WScript.Echo(isNaN(a)); // Prints: -1 (a evaluates to NaN!) WScript.Echo(1*a); // Prints: -1.#IND (in Echo output this means NaN) WScript.Echo(b >= b); // Prints: -1 (as expected) WScript.Echo(isNaN(b)); // Prints: 0 (b evaluates to a valid number) WScript.Echo(1*b); // Prints: 0 (b evaluates to 0) WScript.Echo(a >= 0 && a <= 0); // Prints: 0 (as expected) WScript.Echo(a == 0); // Prints: 0 (as expected) WScript.Echo(b >= 0 && b <= 0); // Prints: -1 (as expected) WScript.Echo(b == 0); // Prints: 0 (!!!)
当在算术表达式或数值比较中使用null时,它变为0-与false类似,它基本上只是一种特殊的“零”。 另外一方面, undefined是真正的“无”,当您尝试在数字上下文中使用它时,它会变为NaN (“非数字”)。
请注意, null和undefined从==
和!=
运算符处获得特殊处理,可是您可使用表达式(a >= b && a <= b)
来测试a和b的真数值相等性。