undefined

undefined 全局属性表示原始值 undefined。它是 JavaScript 的 primitive types 之一。

¥The undefined global property represents the primitive value undefined. It is one of JavaScript's primitive types.

Try it

¥Value

原始值 undefined

¥The primitive value undefined.

Property attributes of undefined
Writable no
Enumerable no
Configurable no

描述

¥Description

undefined 是全局对象的属性。也就是说,它是全局范围内的变量。

¥undefined is a property of the global object. That is, it is a variable in global scope.

在所有非旧版浏览器中,undefined 是不可配置、不可写的属性。即使情况并非如此,也请避免覆盖它。

¥In all non-legacy browsers, undefined is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.

未赋值的变量的类型为 undefined。如果正在计算的变量没有指定值,则方法或语句也会返回 undefined。如果值不是 returned,则函数返回 undefined

¥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.

注意:虽然你可以在全局作用域以外的任何作用域中使用 undefined 作为 identifier(变量名)(因为 undefined 不是 保留字),但这样做是一个非常糟糕的主意,它会使你的代码难以维护和调试。

¥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.

js
// DON'T DO THIS

(() => {
  const undefined = "foo";
  console.log(undefined, typeof undefined); // foo string
})();

((undefined) => {
  console.log(undefined, typeof undefined); // foo string
})("foo");

示例

¥Examples

严格平等和未定义

¥Strict equality and undefined

你可以使用 undefined 以及严格相等和不等运算符来确定变量是否具有值。在以下代码中,变量 x 未初始化,并且 if 语句的计算结果为 true。

¥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.

js
let x;
if (x === undefined) {
  // these statements execute
} else {
  // these statements do not execute
}

注意:这里必须使用严格相等运算符(而不是标准相等运算符),因为 x == undefined 还会检查 x 是否为 null,而严格相等则不会。这是因为 null 不等于 undefined

¥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 Equality comparison and sameness for details.

typeof 运算符和未定义

¥typeof operator and undefined

或者,可以使用 typeof

¥Alternatively, typeof can be used:

js
let x;
if (typeof x === "undefined") {
  // these statements execute
}

使用 typeof 的原因之一是,如果变量尚未声明,它不会引发错误。

¥One reason to use typeof is that it does not throw an error if the variable has not been declared.

js
// x has not been declared before
// evaluates to true without errors
if (typeof x === "undefined") {
  // these statements execute
}

// Throws a ReferenceError
if (x === undefined) {
}

然而,还有另一种选择。JavaScript 是一种静态作用域语言,因此可以通过查看变量是否在封闭上下文中声明来了解变量是否已声明。

¥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.

全局作用域绑定到 global object,因此可以通过使用 in 运算符检查全局对象上的属性是否存在来检查全局上下文中变量是否存在,例如:

¥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:

js
if ("x" in window) {
  // These statements execute only if x is defined globally
}

void 运算符和未定义

¥void operator and undefined

void 运算符是第三种选择。

¥The void operator is a third alternative.

js
let 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
}

规范

Specification
ECMAScript Language Specification
# sec-undefined

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看