Skip to content

prefer-const

Reports a let variable that is assigned once and could be a const: the single write is its initializer, a for-in or for-of head, or the first assignment statement in the same block as the declaration. A variable written in a nested block or function, a let of a for (;;) head, and a destructuring that assigns members or outer names are not reported.

A let says the variable changes. When it never does, the keyword misleads the reader into looking for the later assignment, and const lets the compiler stop one.

To fix, write const.

Reported
...
let b = 2;
let c = 3;
c = 4;
function f() {
...
function g() {
let d;
d = 1;
return d;
}
js:
lint:
rules:
prefer-const: warn # off (default), warn, error

Enabled by the presets: js/typescript-eslint-recommended, js/biome-recommended.

Suppress one line with sudsy-disable-next-line prefer-const in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint prefer-const partial the options keep their defaults (destructuring: any, ignoreReadBeforeAssign: false)