top | item 28838212

(no title)

keawade | 4 years ago

That’s a JavaScript specific nuance that typescript inherits.

‘const’ declares a variable with an immutable reference, not an immutable value. If you’re referencing a simple literal like a string or a number that’s effectively the same thing but for objects (and arrays under the hood of JavaScript are fancy objects) while the reference to your given object is constant, the properties of that objects are still mutable.

discuss

order

Xenoamorphous|4 years ago

Same as Java’s final, isn’t it. You can use final when declaring a reference to an object but that doesn’t make the object itself immutable, just the reference itself.

AtNightWeCode|4 years ago

Same in a lot of programming languages. A common source of errors too. Maybe it is a bit more confusing in JS simply cause people have been taught to make an active choice between var, let and const.

shadowgovt|4 years ago

Correct. This is a thing you can do in JavaScript (and TypeScript) that sometimes trips new people up but is exactly what these keywords mean:

  const foo = [];
  foo.push(1,2,3);