12 If a variable is never reassigned, using the `const` declaration is better.
14 `const` declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
18 This rule is aimed at flagging variables that are declared using `let` keyword, but never reassigned after the initial assignment.
20 Examples of **incorrect** code for this rule:
25 /*eslint prefer-const: "error"*/
27 // it's initialized and never reassigned.
43 // `i` is redefined (not reassigned) on each loop step.
44 for (let i in [1, 2, 3]) {
48 // `a` is redefined (not reassigned) on each loop step.
49 for (let a of [1, 2, 3]) {
56 Examples of **correct** code for this rule:
61 /*eslint prefer-const: "error"*/
66 // it's never initialized.
70 // it's reassigned after initialized.
76 // it's initialized in a different block from the declaration.
83 // it's initialized in a different scope.
92 // it's initialized at a place that we cannot write a variable declaration.
97 // `i` gets a new binding each iteration
98 for (const i in [1, 2, 3]) {
102 // `a` gets a new binding each iteration
103 for (const a of [1, 2, 3]) {
107 // `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
108 for (let i = 0, end = 10; i < end; ++i) {
112 // `predicate` is only assigned once but cannot be separately declared as `const`
114 [object.type, predicate] = foo();
116 // `a` is only assigned once but cannot be separately declared as `const`
119 ({ a, c: b.c } = func());
121 // suggest to use `no-var` rule.
132 "prefer-const": ["error", {
133 "destructuring": "any",
134 "ignoreReadBeforeAssign": false
141 The kind of the way to address variables in destructuring.
144 * `"any"` (default) - If any variables in destructuring should be `const`, this rule warns for those variables.
145 * `"all"` - If all variables in destructuring should be `const`, this rule warns the variables. Otherwise, ignores them.
147 Examples of **incorrect** code for the default `{"destructuring": "any"}` option:
152 /*eslint prefer-const: "error"*/
155 let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/
161 Examples of **correct** code for the default `{"destructuring": "any"}` option:
166 /*eslint prefer-const: "error"*/
170 const {a: a0, b} = obj;
173 // all variables are reassigned.
181 Examples of **incorrect** code for the `{"destructuring": "all"}` option:
186 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
189 // all of `a` and `b` should be const, so those are warned.
190 let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead.
191 'b' is never reassigned, use 'const' instead.*/
196 Examples of **correct** code for the `{"destructuring": "all"}` option:
201 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
204 // 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
211 ### ignoreReadBeforeAssign
213 This is an option to avoid conflicting with `no-use-before-define` rule (without `"nofunc"` option).
214 If `true` is specified, this rule will ignore variables that are read between the declaration and the first assignment.
217 Examples of **correct** code for the `{"ignoreReadBeforeAssign": true}` option:
222 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
226 function initialize() {
228 clearInterval(timer);
231 timer = setInterval(initialize, 100);
236 Examples of **correct** code for the default `{"ignoreReadBeforeAssign": false}` option:
241 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
244 const timer = setInterval(initialize, 100);
245 function initialize() {
247 clearInterval(timer);
254 ## When Not To Use It
256 If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.