]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-undef-init.md
import 8.3.0 source
[pve-eslint.git] / eslint / docs / rules / no-undef-init.md
1 # Disallow Initializing to undefined (no-undef-init)
2
3 In JavaScript, a variable that is declared and not initialized to any value automatically gets the value of `undefined`. For example:
4
5 ```js
6 var foo;
7
8 console.log(foo === undefined); // true
9 ```
10
11 It's therefore unnecessary to initialize a variable to `undefined`, such as:
12
13 ```js
14 var foo = undefined;
15 ```
16
17 It's considered a best practice to avoid initializing variables to `undefined`.
18
19
20 ## Rule Details
21
22 This rule aims to eliminate `var` and `let` variable declarations that initialize to `undefined`.
23
24 Examples of **incorrect** code for this rule:
25
26 ```js
27 /*eslint no-undef-init: "error"*/
28
29 var foo = undefined;
30 let bar = undefined;
31 ```
32
33 Examples of **correct** code for this rule:
34
35 ```js
36 /*eslint no-undef-init: "error"*/
37
38 var foo;
39 let bar;
40 ```
41
42 Please note that this rule does not check `const` declarations, destructuring patterns, function parameters, and class fields.
43
44 Examples of additional **correct** code for this rule:
45
46 ```js
47 /*eslint no-undef-init: "error"*/
48
49 const foo = undefined;
50
51 let { bar = undefined } = baz;
52
53 [quux = undefined] = quuux;
54
55 (foo = undefined) => {};
56
57 class Foo {
58 bar = undefined;
59 }
60 ```
61
62 ## When Not To Use It
63
64 There is one situation where initializing to `undefined` behaves differently than omitting the initialization, and that's when a `var` declaration occurs inside of a loop. For example:
65
66 Example of **incorrect** code for this rule:
67
68 ```js
69 for (i = 0; i < 10; i++) {
70 var x = undefined;
71 console.log(x);
72 x = i;
73 }
74 ```
75
76 In this case, the `var x` is hoisted out of the loop, effectively creating:
77
78 ```js
79 var x;
80
81 for (i = 0; i < 10; i++) {
82 x = undefined;
83 console.log(x);
84 x = i;
85 }
86 ```
87
88 If you were to remove the initialization, then the behavior of the loop changes:
89
90 ```js
91 for (i = 0; i < 10; i++) {
92 var x;
93 console.log(x);
94 x = i;
95 }
96 ```
97
98 This code is equivalent to:
99
100 ```js
101 var x;
102
103 for (i = 0; i < 10; i++) {
104 console.log(x);
105 x = i;
106 }
107 ```
108
109 This produces a different outcome than defining `var x = undefined` in the loop, as `x` is no longer reset to `undefined` each time through the loop.
110
111 If you're using such an initialization inside of a loop, then you should disable this rule.
112
113 Example of **correct** code for this rule, because it is disabled on a specific line:
114
115 ```js
116 /*eslint no-undef-init: "error"*/
117
118 for (i = 0; i < 10; i++) {
119 var x = undefined; // eslint-disable-line no-undef-init
120 console.log(x);
121 x = i;
122 }
123 ```
124
125 ## Related Rules
126
127 * [no-undefined](no-undefined.md)
128 * [no-void](no-void.md)