const

ES6

const isn't about protected, frozen, constant or immutable variables. The only constant is the binding, it's a immutable reference to a binding.

Like let, const's are block scope.

const foo = [];
foo.push({something: 'new'});
console.log(foo); // [{ someting: 'new' }]

If a const is a complex variable like an array or object, it can still have it's contents modified.

It just can't be reassigned...

const foo = [];
foo = 1;
// Uncaught TypeError: Assignment to constant variable.

Like let's const's are also block scoped

What's the point?

Whilst it doesn't make a variable immutable, it's still very useful for showing purpose and can easily be linted.

Don't rely on it for behaviour

Think about it more as signalling intent

See Object.freeze for objects, or Symbols for strings

results matching ""

    No results matching ""