]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/prefer-const.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / prefer-const.md
CommitLineData
8f9d1d4d
DC
1---
2title: prefer-const
8f9d1d4d
DC
3rule_type: suggestion
4related_rules:
5- no-var
6- no-use-before-define
7---
8
9
eb39fafa
DC
10
11If a variable is never reassigned, using the `const` declaration is better.
12
13`const` declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
14
15## Rule Details
16
17This rule is aimed at flagging variables that are declared using `let` keyword, but never reassigned after the initial assignment.
18
19Examples of **incorrect** code for this rule:
20
8f9d1d4d
DC
21::: incorrect
22
eb39fafa
DC
23```js
24/*eslint prefer-const: "error"*/
eb39fafa
DC
25
26// it's initialized and never reassigned.
27let a = 3;
28console.log(a);
29
30let a;
31a = 0;
32console.log(a);
33
609c276f
TL
34class C {
35 static {
36 let a;
37 a = 0;
38 console.log(a);
39 }
40}
41
eb39fafa
DC
42// `i` is redefined (not reassigned) on each loop step.
43for (let i in [1, 2, 3]) {
44 console.log(i);
45}
46
47// `a` is redefined (not reassigned) on each loop step.
48for (let a of [1, 2, 3]) {
49 console.log(a);
50}
51```
52
8f9d1d4d
DC
53:::
54
eb39fafa
DC
55Examples of **correct** code for this rule:
56
8f9d1d4d
DC
57::: correct
58
eb39fafa
DC
59```js
60/*eslint prefer-const: "error"*/
eb39fafa
DC
61
62// using const.
63const a = 0;
64
65// it's never initialized.
66let a;
67console.log(a);
68
69// it's reassigned after initialized.
70let a;
71a = 0;
72a = 1;
73console.log(a);
74
75// it's initialized in a different block from the declaration.
76let a;
77if (true) {
78 a = 0;
79}
80console.log(a);
81
609c276f
TL
82// it's initialized in a different scope.
83let a;
84class C {
85 #x;
86 static {
87 a = obj => obj.#x;
88 }
89}
90
eb39fafa
DC
91// it's initialized at a place that we cannot write a variable declaration.
92let a;
93if (true) a = 0;
94console.log(a);
95
96// `i` gets a new binding each iteration
97for (const i in [1, 2, 3]) {
98 console.log(i);
99}
100
101// `a` gets a new binding each iteration
102for (const a of [1, 2, 3]) {
103 console.log(a);
104}
105
106// `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
107for (let i = 0, end = 10; i < end; ++i) {
f2a92ac6 108 console.log(i);
eb39fafa
DC
109}
110
111// `predicate` is only assigned once but cannot be separately declared as `const`
112let predicate;
113[object.type, predicate] = foo();
114
115// `a` is only assigned once but cannot be separately declared as `const`
116let a;
117const b = {};
118({ a, c: b.c } = func());
119
120// suggest to use `no-var` rule.
121var b = 3;
122console.log(b);
123```
124
8f9d1d4d
DC
125:::
126
eb39fafa
DC
127## Options
128
129```json
130{
131 "prefer-const": ["error", {
132 "destructuring": "any",
133 "ignoreReadBeforeAssign": false
134 }]
135}
136```
137
138### destructuring
139
140The kind of the way to address variables in destructuring.
141There are 2 values:
142
143* `"any"` (default) - If any variables in destructuring should be `const`, this rule warns for those variables.
144* `"all"` - If all variables in destructuring should be `const`, this rule warns the variables. Otherwise, ignores them.
145
146Examples of **incorrect** code for the default `{"destructuring": "any"}` option:
147
8f9d1d4d
DC
148::: incorrect
149
eb39fafa
DC
150```js
151/*eslint prefer-const: "error"*/
152/*eslint-env es6*/
153
154let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/
155a = a + 1;
156```
157
8f9d1d4d
DC
158:::
159
eb39fafa
DC
160Examples of **correct** code for the default `{"destructuring": "any"}` option:
161
8f9d1d4d
DC
162::: correct
163
eb39fafa
DC
164```js
165/*eslint prefer-const: "error"*/
166/*eslint-env es6*/
167
168// using const.
169const {a: a0, b} = obj;
170const a = a0 + 1;
171
172// all variables are reassigned.
173let {a, b} = obj;
174a = a + 1;
175b = b + 1;
176```
177
8f9d1d4d
DC
178:::
179
eb39fafa
DC
180Examples of **incorrect** code for the `{"destructuring": "all"}` option:
181
8f9d1d4d
DC
182::: incorrect
183
eb39fafa
DC
184```js
185/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
186/*eslint-env es6*/
187
188// all of `a` and `b` should be const, so those are warned.
189let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead.
190 'b' is never reassigned, use 'const' instead.*/
191```
192
8f9d1d4d
DC
193:::
194
eb39fafa
DC
195Examples of **correct** code for the `{"destructuring": "all"}` option:
196
8f9d1d4d
DC
197::: correct
198
eb39fafa
DC
199```js
200/*eslint prefer-const: ["error", {"destructuring": "all"}]*/
201/*eslint-env es6*/
202
203// 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
204let {a, b} = obj;
205a = a + 1;
206```
207
8f9d1d4d
DC
208:::
209
eb39fafa
DC
210### ignoreReadBeforeAssign
211
212This is an option to avoid conflicting with `no-use-before-define` rule (without `"nofunc"` option).
213If `true` is specified, this rule will ignore variables that are read between the declaration and the first assignment.
214Default is `false`.
215
216Examples of **correct** code for the `{"ignoreReadBeforeAssign": true}` option:
217
8f9d1d4d
DC
218::: correct
219
eb39fafa
DC
220```js
221/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
222/*eslint-env es6*/
223
224let timer;
225function initialize() {
226 if (foo()) {
227 clearInterval(timer);
228 }
229}
230timer = setInterval(initialize, 100);
231```
232
8f9d1d4d
DC
233:::
234
eb39fafa
DC
235Examples of **correct** code for the default `{"ignoreReadBeforeAssign": false}` option:
236
8f9d1d4d
DC
237::: correct
238
eb39fafa
DC
239```js
240/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
241/*eslint-env es6*/
242
243const timer = setInterval(initialize, 100);
244function initialize() {
245 if (foo()) {
246 clearInterval(timer);
247 }
248}
249```
250
8f9d1d4d
DC
251:::
252
eb39fafa
DC
253## When Not To Use It
254
255If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.