11 If a variable is never reassigned, using the `const` declaration is better.
13 `const` declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
17 This rule is aimed at flagging variables that are declared using `let` keyword, but never reassigned after the initial assignment.
19 Examples of **incorrect** code for this rule:
24 /*eslint prefer-const: "error"*/
26 // it's initialized and never reassigned.
42 // `i` is redefined (not reassigned) on each loop step.
43 for (let i in [1, 2, 3]) {
47 // `a` is redefined (not reassigned) on each loop step.
48 for (let a of [1, 2, 3]) {
55 Examples of **correct** code for this rule:
60 /*eslint prefer-const: "error"*/
65 // it's never initialized.
69 // it's reassigned after initialized.
75 // it's initialized in a different block from the declaration.
82 // it's initialized in a different scope.
91 // it's initialized at a place that we cannot write a variable declaration.
96 // `i` gets a new binding each iteration
97 for (const i in [1, 2, 3]) {
101 // `a` gets a new binding each iteration
102 for (const a of [1, 2, 3]) {
106 // `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
107 for (let i = 0, end = 10; i < end; ++i) {
111 // `predicate` is only assigned once but cannot be separately declared as `const`
113 [object.type, predicate] = foo();
115 // `a` is only assigned once but cannot be separately declared as `const`
118 ({ a, c: b.c } = func());
120 // suggest to use `no-var` rule.
131 "prefer-const": ["error", {
132 "destructuring": "any",
133 "ignoreReadBeforeAssign": false
140 The kind of the way to address variables in destructuring.
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.
146 Examples of **incorrect** code for the default `{"destructuring": "any"}` option:
151 /*eslint prefer-const: "error"*/
154 let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/
160 Examples of **correct** code for the default `{"destructuring": "any"}` option:
165 /*eslint prefer-const: "error"*/
169 const {a: a0, b} = obj;
172 // all variables are reassigned.
180 Examples of **incorrect** code for the `{"destructuring": "all"}` option:
185 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
188 // all of `a` and `b` should be const, so those are warned.
189 let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead.
190 'b' is never reassigned, use 'const' instead.*/
195 Examples of **correct** code for the `{"destructuring": "all"}` option:
200 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
203 // 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
210 ### ignoreReadBeforeAssign
212 This is an option to avoid conflicting with `no-use-before-define` rule (without `"nofunc"` option).
213 If `true` is specified, this rule will ignore variables that are read between the declaration and the first assignment.
216 Examples of **correct** code for the `{"ignoreReadBeforeAssign": true}` option:
221 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
225 function initialize() {
227 clearInterval(timer);
230 timer = setInterval(initialize, 100);
235 Examples of **correct** code for the default `{"ignoreReadBeforeAssign": false}` option:
240 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
243 const timer = setInterval(initialize, 100);
244 function initialize() {
246 clearInterval(timer);
253 ## When Not To Use It
255 If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.