Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

There's the const definition in Exploring ES6 by Dr. Axel Rauschmayer:

const works like let, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards. […]

const bar = 123;
bar = 456;  // TypeError: `bar` is read-only

and then he writes

Pitfall: const does not make the value immutable

const only means that a variable always has the same value, but it does not mean that the value itself is or becomes immutable.

I am little confused with this pitfall. Can any one clearly define the const with this pitfall?

share|improve this question
21  
The MDN explanation is clear: "The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in case the content is an object, this means the object itself can still be altered." (emphasis mine) – Gerardo Furtado yesterday
4  
It means that if the value is mutable, (e.g. if it is an object) then you can still mutate that object (e.g. update, add, remove properties). – Felix Kling yesterday
2  
const x = "immutable" is immutable, because String is immutable. const prohibits merely reassignment. – ftor yesterday
3  
@ibrahimmahrir: passing a reference (what JavaScript does for objects) is not the same as pass by reference (which describes the relationship between bindings, the value is irrelevant). – Felix Kling yesterday
2  
@ibrahimmahrir: Yes, that's the issue. The term pass-by-reference has a very specific meaning. – Felix Kling yesterday
up vote 49 down vote accepted

MDN sums it up nicely:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in case the content is an object, this means the object itself can still be altered.

More succinctly: const creates an immutable binding.

In other words: const, like var, gives you a mutable chunk of memory in which you're storing something. However, const dictates that you must keep referring to that same chunk of memory – you can't reassign the variable to a different chunk of memory, because the variable reference is constant.

To really make something constant and unchanging after you've declared it, you need to use something like Object.freeze(). However, that's shallow and only works on key/value pairs. Freezing an entire object takes a bit more effort. To do so repeatedly in a performant way is yet more challenging. If you really have a need for that, I'd recommend checking out something like Immutable.js

share|improve this answer
9  
In C terms: If a normal var x is a struct Object *x, a const x is a struct Object *const x. The pointer can't be changed; the thing it's pointing to can. – QPaysTaxes 21 hours ago

When you make something const in JavaScript, you can't reassign the variable itself to reference something else. However, the variable can still reference a mutable object.

const x = {a: 123};

// This is not allowed.  This would reassign `x` itself to refer to a
// different object.
x = {b: 456};

// This, however, is allowed.  This would mutate the object `x` refers to,
// but `x` itself hasn't been reassigned to refer to something else.
x.a = 456;

In the case of primitives such as strings and numbers, const is simpler to understand, since you don't mutate the values but instead assign a new value to the variable.

share|improve this answer
5  
This answer is much better than the accepted one. More succinct and contains actual example code. (In other words, gets to the point.) +1 – jpmc26 yesterday

Rebinding

const and let declarations control whether rebindings (aka reassignments) between identifiers and values are allowed:

const x = "initial value";
let y = "initial value";

// rebinding/reassignment

try { x = "reassignment" } catch(e) { console.log(x) } // fails

y = "reassignment"; // succeeds
console.log(y);

Immutability

Immutability is controlled at the type level. Object is a mutable type, whereas String is an immutable type:

const o = {mutable: true};
const x = "immutable";

// mutations

o.foo = true; // succeeds
x[0] = "I"; // fails

console.log(o); // {mutable: true, foo: true}
console.log(x); // immutable

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.