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