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