小结:
1、
把 null 作为尚未创建的对象
可以使用undefined和严格相等或不相等操作符来决定一个变量是否拥有值
undefined - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
Description
undefined
is a property of the global object. That is, it is a variable in global scope. The initial value of undefined
is the primitive value undefined
.
In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined
is a non-configurable, non-writable property, per the ECMAScript 5 specification. (Even when this is not the case, avoid overriding it.)
A variable that has not been assigned a value is of type undefined
. A method or statement also returns undefined
if the variable that is being evaluated does not have an assigned value. A function returns undefined
if a value was not returned
.
Note: While you can use undefined
as an identifier (variable name) in any scope other than the global scope (because undefined
is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.
// DON'T DO THIS
// logs "foo string"
(function() {
var undefined = 'foo';
console.log(undefined, typeof undefined);
})();
// logs "foo string"
(function(undefined) {
console.log(undefined, typeof undefined);
})('foo');
Examples
Strict equality and undefined
You can use undefined
and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x
is not initialized, and the if
statement evaluates to true.
var x;
if (x === undefined) {
// these statements execute
}
else {
// these statements do not execute
}
Note: The strict equality operator (as opposed to the standard equality operator) must be used here, because x == undefined
also checks whether x
is null
, while strict equality doesn't. This is because null
is not equivalent to undefined
.
See comparison operators for details.
typeof operator and undefined
Alternatively, typeof
can be used:
var x;
if (typeof x === 'undefined') {
// these statements execute
}
One reason to use typeof
is that it does not throw an error if the variable has not been declared.
// x has not been declared before
if (typeof x === 'undefined') { // evaluates to true without errors
// these statements execute
}
if (x === undefined) { // throws a ReferenceError
}
However, there is another alternative. JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context.
The global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object, using the in
operator, for instance:
if ('x' in window) {
// these statements execute only if x is defined globally
}
void operator and undefined
The void
operator is a third alternative.
var x;
if (x === void 0) {
// these statements execute
}
// y has not been declared before
if (y === void 0) {
// throws Uncaught ReferenceError: y is not defined
}
null - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
Description
The value null
is written with a literal: null
. null
is not an identifier for a property of the global object, like undefined
can be. Instead, null
expresses a lack of identification, indicating that a variable points to no object. In APIs, null
is often retrieved in a place where an object can be expected but no object is relevant.
// foo does not exist. It is not defined and has never been initialized:
foo; //ReferenceError: foo is not defined
// foo is known to exist now but it has no type or value:
var foo = null;
foo; //null
Examples
Difference between null
and undefined
When checking for null
or undefined
, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion.
typeof null // "object" (not "null" for legacy reasons)
typeof undefined // "undefined"
null === undefined // false
null == undefined // true
null === null // true
null == null // true
!null // true
isNaN(1 + null) // false
isNaN(1 + undefined) // true
Specifications
Specification |
---|
ECMAScript Language Specification # sec-null-value |
描述
值 null
是一个字面量,不像 undefined
,它不是全局对象的一个属性。null
是表示缺少的标识,指示变量未指向任何对象。把 null
作为尚未创建的对象,也许更好理解。在 API 中,null
常在返回类型应是一个对象,但没有关联的值的地方使用。
// foo 不存在,它从来没有被定义过或者是初始化过:
foo;
"ReferenceError: foo is not defined"
// foo 现在已经是知存在的,但是它没有类型或者是值:
var foo = null;
foo;
null
null
与 undefined
的不同点:
当检测 null
或 undefined
时,注意相等(==)与全等(===)两个操作符的区别 ,前者会执行类型转换:
typeof null // "object" (因为一些以前的原因而不是'null')
typeof undefined // "undefined"
null === undefined // false
null == undefined // true
null === null // true
null == null // true
!null //true
isNaN(1 + null) // false
isNaN(1 + undefined) // true
描述
undefined
是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined
的最初值就是原始数据类型undefined
。
在现代浏览器(JavaScript 1.8.5/Firefox 4+),自ECMAscript5标准以来undefined是一个不能被配置(non-configurable),不能被重写(non-writable)的属性。即便事实并非如此,也要避免去重写它。
一个没有被赋值的变量的类型是undefined。如果方法或者是语句中操作的变量没有被赋值,则会返回undefined(对于这句话持疑惑态度,请查看英文原文来理解)。
function test(a){
console.log(typeof a); // undefined
return a;
}
test(); // 返回"undefined"
一个函数如果没有使用return语句指定返回
值,就会返回一个undefined值。
警告:但是它有可能在非全局作用域中被当作标识符(变量名)来使用(因为undefined不是一个保留字
(en-US))),这样做是一个非常坏的主意,因为这样会使你的代码难以去维护和排错。
// 不要这样做!
// 打印 'foo string' PS:说明undefined的值和类型都已经改变
(function() {
var undefined = 'foo';
console.log(undefined, typeof undefined)
})()
// 打印 'foo string' PS:说明undefined的值和类型都已经改变
(function(undefined) {
console.log(undefined, typeof undefined)
})('foo')
示例
严格相等和undefined
你可以使用undefined和严格相等或不相等操作符来决定一个变量是否拥有值。在下面的代码中,变量x是未定义的,if 语句的求值结果将是true
var x;
if (x === undefined) {
// 执行这些语句
} else {
// 这些语句不会被执行
}
备注:这里是必须使用严格相等操作符(===)而不是标准相等操作符(==),因为 x == undefined 会检查x是不是null,但是严格相等不会检查(有点饶人,其实 === 会严格判断双方的类型、值等是否相等)。null不等同于undefined。移步比较操作符
(en-US)查看详情。
Typeof 操作符和undefined
或者,可以使用typeof
:
var x;
if(typeof x === 'undefined') {
// 执行这些语句
}
使用 typeof
的原因是它不会在一个变量没有被声明的时候抛出一个错误。
// 这里没有声明y
if(typeof y === 'undefined') { // 没有错误,执行结果为true
console.log("y is " + typeof y ) // y is undefined
}
if(y === undefined) { // ReferenceError: y is not defined
}
但是,技术方面看来这样的使用方法应该被避免。JavaScript是一个静态作用域语言,所以,一个变量是否被声明可以通过看它是否在一个封闭的上下文中被声明。唯一的例外是全局作用域,但是全局作用域是被绑定在全局对象上的,所以要检查一个变量是否在全局上下文中存在可以通过检查全局对象上是否存在这个属性(比如使用in
操作符)。
if ('x' in window) {
// 只有x被全局性的定义 才会这行这些语句
}
Void操作符和undefined
void
操作符是第三种可以替代的方法。
var x;
if(x === void 0) {
// 执行这些语句
}
// 没有声明y
if(y === void 0) {
// 抛出一个RenferenceError错误(与`typeof`相比)
}
JavaScript typeof, null, 和 undefined | 菜鸟教程 https://www.runoob.com/js/js-typeof.html
null是一个只有一个值的特殊类型。表示一个空对象引用。
undefined 是一个没有设置值的变量