liveBook · Manning

Binding

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A named program binding that refers to a value and may or may not be reassigned depending on its declaration form.

A binding connects a name to a value. Reassignment changes the value associated with the name; mutation changes an object the binding refers to. section 5.1

Keep these rules distinct:

  • `const` prevents reassignment, but does not prevent changing a mutable object through the binding. section 5.1
  • `let` allows reassignment and is block-scoped, so it cannot be accessed outside its block. section 4.1
  • Use `const` when a name need not be reassigned; use `let` when it does. section 5.2
const mutableConstant = {};

mutableConstant.foo = 'bar';            1
mutableConstant.foo = 'baz';            2

mutableConstant = {foo: 'bat'}          3
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":40},{\"line\":2,\"ch\":41}],[{\"line\":3,\"ch\":40},{\"line\":3,\"ch\":41}],[{\"line\":5,\"ch\":40},{\"line\":5,\"ch\":41}]]"}
!@%STYLE%@!

The property changes mutate the existing object; assigning a new object to the `const` binding is not allowed. section 5.1

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through reassignment, mutation, and block scope without detailing engine internals.
Examples
Uses a constant object whose properties change before reassignment fails, and an array that can be modified without rebinding.
What you can do after
Understand how reassignment differs from mutation when choosing between `const` and `let`.
How it connects
Connects bindings to `let`, `const`, block scope, and mutability.

Also explained in

Compare how 11 books explain binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.