Suggest using const (prefer-const)

If a variable is never modified, using the const declaration is better.

const declaration tells readers, “this variable is never modified,” reducing cognitive load and improving maintainability.

Rule Details

This rule is aimed at flagging variables that are declared using let keyword, but never modified after the initial assignment.

The following patterns are considered warnings:

let a = 3;
for (let i in [1,2,3]) { // `i` is re-defined (not modified) on each loop step.
for (let a of [1,2,3]) { // `a` is re-defined (not modified) on each loop step.

The following patterns are not considered warnings:

let a; // there is no initialization.
for (let i = 0, end = 10; i < end; ++i) { // `end` is never modified, but we cannot separate the declarations without modifying the scope.
var a = 3; // suggest to use `no-var` rule.

When Not to Use It

If you don’t want to be notified about variables that are never modified after initial assignment, you can safely disable this rule.


This rule was introduced in ESLint 0.23.0.