在javascript中声明一个变量而未赋值时,值会被设定为undefined,试图取得一个未定义变量会导致一个控制台ReferenceError
例外错误被抛出:
var a; console.log("The value of a is " + a); // logs "The value of a is undefined" console.log("The value of b is " + b); // throws ReferenceError exception
未定义的undefined
值在布尔类型下会被当作false,
数值类型下undefined
值会被转换为NaN:
var a; a + 2 = NaN
当你对一个空变量求值时,空值null在数值型下会被当作0来对待,而布尔类型下会被当作false。例如:
var n = null; console.log(n * 32); // logs 0