JavaScript

JavaScript

Made by DeepSource

Found reassigning const variables JS-0230

Bug risk
Major

Re-assigning a variable that was declared with the const keyword can lead to a TypeError.

const pi = 3.14159
pi = 3.14 // Uncaught TypeError: Assignment to constant variable.

Bad Practice

const a = 0;
a = 1;

const b = 0;
b += 1;

const c = 0;
++c;

Recommended

const a = 0;
console.log(a);

for (const k in [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(k);
}

for (const x of [1, 2, 3]) { // `a` is re-defined (not modified) on each loop step.
    console.log(x);
}