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.
const a = 0;
a = 1;
const b = 0;
b += 1;
const c = 0;
++c;
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);
}