1 # Suggest using `const` (prefer-const)
3 If a variable is never reassigned, using the `const` declaration is better.
5 `const` declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
9 This rule is aimed at flagging variables that are declared using `let` keyword, but never reassigned after the initial assignment.
11 Examples of **incorrect** code for this rule:
14 /*eslint prefer-const: "error"*/
16 // it's initialized and never reassigned.
32 // `i` is redefined (not reassigned) on each loop step.
33 for (let i in [1, 2, 3]) {
37 // `a` is redefined (not reassigned) on each loop step.
38 for (let a of [1, 2, 3]) {
43 Examples of **correct** code for this rule:
46 /*eslint prefer-const: "error"*/
51 // it's never initialized.
55 // it's reassigned after initialized.
61 // it's initialized in a different block from the declaration.
68 // it's initialized in a different scope.
77 // it's initialized at a place that we cannot write a variable declaration.
82 // `i` gets a new binding each iteration
83 for (const i in [1, 2, 3]) {
87 // `a` gets a new binding each iteration
88 for (const a of [1, 2, 3]) {
92 // `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
93 for (let i = 0, end = 10; i < end; ++i) {
97 // `predicate` is only assigned once but cannot be separately declared as `const`
99 [object.type, predicate] = foo();
101 // `a` is only assigned once but cannot be separately declared as `const`
104 ({ a, c: b.c } = func());
106 // suggest to use `no-var` rule.
115 "prefer-const": ["error", {
116 "destructuring": "any",
117 "ignoreReadBeforeAssign": false
124 The kind of the way to address variables in destructuring.
127 * `"any"` (default) - If any variables in destructuring should be `const`, this rule warns for those variables.
128 * `"all"` - If all variables in destructuring should be `const`, this rule warns the variables. Otherwise, ignores them.
130 Examples of **incorrect** code for the default `{"destructuring": "any"}` option:
133 /*eslint prefer-const: "error"*/
136 let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/
140 Examples of **correct** code for the default `{"destructuring": "any"}` option:
143 /*eslint prefer-const: "error"*/
147 const {a: a0, b} = obj;
150 // all variables are reassigned.
156 Examples of **incorrect** code for the `{"destructuring": "all"}` option:
159 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
162 // all of `a` and `b` should be const, so those are warned.
163 let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead.
164 'b' is never reassigned, use 'const' instead.*/
167 Examples of **correct** code for the `{"destructuring": "all"}` option:
170 /*eslint prefer-const: ["error", {"destructuring": "all"}]*/
173 // 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
178 ### ignoreReadBeforeAssign
180 This is an option to avoid conflicting with `no-use-before-define` rule (without `"nofunc"` option).
181 If `true` is specified, this rule will ignore variables that are read between the declaration and the first assignment.
184 Examples of **correct** code for the `{"ignoreReadBeforeAssign": true}` option:
187 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/
191 function initialize() {
193 clearInterval(timer);
196 timer = setInterval(initialize, 100);
199 Examples of **correct** code for the default `{"ignoreReadBeforeAssign": false}` option:
202 /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/
205 const timer = setInterval(initialize, 100);
206 function initialize() {
208 clearInterval(timer);
213 ## When Not To Use It
215 If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule.
219 * [no-var](no-var.md)
220 * [no-use-before-define](no-use-before-define.md)