]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-undefined.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / rules / no-undefined.md
CommitLineData
eb39fafa
DC
1# Disallow Use of `undefined` Variable (no-undefined)
2
3The `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:
4
5```js
6function doSomething(data) {
7 var undefined = "hi";
8
9 // doesn't do what you think it does
10 if (data === undefined) {
11 // ...
12 }
13
14}
15```
16
17Because `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:
18
19* Variables that should be `undefined` are simply left uninitialized. (All uninitialized variables automatically get the value of `undefined` in JavaScript.)
20* Checking if a value is `undefined` should be done with `typeof`.
21* Using the `void` operator to generate the value of `undefined` if necessary.
22
23As an alternative, you can use the [no-global-assign](no-global-assign.md) and [no-shadow-restricted-names](no-shadow-restricted-names.md) rules to prevent `undefined` from being shadowed or assigned a different value. This ensures that `undefined` will always hold its original, expected value.
24
eb39fafa
DC
25## Rule Details
26
27This rule aims to eliminate the use of `undefined`, and as such, generates a warning whenever it is used.
28
29Examples of **incorrect** code for this rule:
30
31```js
32/*eslint no-undefined: "error"*/
33
34var foo = undefined;
35
36var undefined = "foo";
37
38if (foo === undefined) {
39 // ...
40}
41
42function foo(undefined) {
43 // ...
44}
45```
46
47Examples of **correct** code for this rule:
48
49```js
50/*eslint no-undefined: "error"*/
51
52var foo = void 0;
53
54var Undefined = "foo";
55
56if (typeof foo === "undefined") {
57 // ...
58}
59
60global.undefined = "foo";
61```
62
63## When Not To Use It
64
65If you want to allow the use of `undefined` in your code, then you can safely turn this rule off.
66
67## Further Reading
68
69* [undefined - JavaScript \| MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)
70* [Understanding JavaScript’s ‘undefined’ \| JavaScript, JavaScript...](https://javascriptweblog.wordpress.com/2010/08/16/understanding-undefined-and-preventing-referenceerrors/)
71* [ECMA262 edition 5.1 §15.1.1.3: undefined](https://es5.github.io/#x15.1.1.3)
72
73## Related Rules
74
75* [no-undef-init](no-undef-init.md)
76* [no-void](no-void.md)
77* [no-shadow-restricted-names](no-shadow-restricted-names.md)
78* [no-global-assign](no-global-assign.md)