Disallow Use of undefined Variable (no-undefined)

不允许使用undefined变量 (no-undefined)

The undefined variable in JavaScript is actually a property of the global object. As such, in ECMAScript 3 it was possible to overwrite the value of undefined. While ECMAScript 5 disallows overwriting undefined, it’s still possible to shadow undefined, such as:

undefined 变量在 JavaScript 中是独一无二的,因为它实际上是一个全局对象属性。在 ECMAScript 3 中,可重写 undefined 的值,然而 ECMAScript 5 不允许重写 undefined ,但仍然可能遮盖原来的 undefined,例如:

function doSomething(data) {
    var undefined = "hi";

    // doesn't do what you think it does
    if (data === undefined) {
        // ...


Because undefined can be overwritten or shadowed, reading undefined can give an unexpected value. (This is not the case for null, which is a keyword that always produces the same value.) To guard against this, you can avoid all uses of undefined, which is what some style guides recommend and what this rule enforces. Those style guides then also recommend:

由于 undefined 会被覆盖和遮蔽,所以读取 undefined 会给你一个意想不到的值。(这种情况还不同于 nullnull 会始终产生同一个值。)为了防止这种情况凡是,你可以避免对 undefined 的所有使用,这也是一项风格指南所推荐的,也是该规则强制的。那些风格指南也推荐:

As an alternative, you can use the no-global-assign and no-shadow-restricted-names rules to prevent undefined from being shadowed or assigned a different value. This ensures that undefined will always hold its original, expected value.

作为另外一种宣泄,你可以使用 no-global-assignno-shadow-restricted-names 规则来阻止遮蔽 undefined 或被赋值为一个不同的值。这会保证 undefined 将总是保持它的原始的和所期望的值。

Rule Details

This rule aims to eliminate the use of undefined, and as such, generates a warning whenever it is used.

此规则的目的在于消除使用 undefined,使用 undefined 会产生一个警告。

Examples of incorrect code for this rule:

错误 代码示例:

/*eslint no-undefined: "error"*/

var foo = undefined;

var undefined = "foo";

if (foo === undefined) {
    // ...

function foo(undefined) {
    // ...

Examples of correct code for this rule:

正确 代码示例:

/*eslint no-undefined: "error"*/

var foo = void 0;

var Undefined = "foo";

if (typeof foo === "undefined") {
    // ...

global.undefined = "foo";

When Not To Use It

If you want to allow the use of undefined in your code, then you can safely turn this rule off.

如果想在代码中使用 undefined,应关闭此规则。

Further Reading


This rule was introduced in ESLint 0.7.1.

该规则在 ESLint 0.7.1 中被引入。