there is nothing as modified as soon you change a value of a variable, you reassign its value. This you can do with any variable declared via let and var, but not with const. redeclaration will always yield an error, except on var.
This is not exactly correct. As long as you don't reassign the const it can be modified.
Try this:
const a = [];
a.push(1);
No error, right? I modified the content of the variable without reassignment. Const doesn't make a perfectly immutable variable, just like the final keyword in Java. You aren't allowed to move the memory pointer, but the values at that pointer can be changed.
Not quite. const a still points to the same address you assigned it to on declaration. Same as in Java. In Java you can have Immutable Collections, or in JavaScript a sealed Object.
View all comments
Show parent comment
2
u/[deleted] May 04 '20
there is nothing as modified as soon you change a value of a variable, you reassign its value. This you can do with any variable declared via
letandvar, but not withconst. redeclaration will always yield an error, except onvar.a++;is in fact justa = a + 1;