liveBook · Manning

Constant

As explained in Secrets of the JavaScript Ninja, Third Edition

const is a JavaScript keyword that declares a block-scoped binding that must be initialized and cannot be reassigned.

A `const` declaration creates a block-scoped binding that must be initialized and cannot be reassigned. Its scope is determined by the nearest enclosing block, such as a function body or standalone block. section 7.3.2 section 7.3.1

console.assert(x === 1, "x is 1");
const x = 1;

This reference reads `x` before its `const` declaration executes, so it triggers a temporal-dead-zone error. section 7.3.2

Scope alone does not make a binding ready to use: execution order matters. A function may refer to `x` before its declaration in the code, provided the function runs only after `x` is initialized. section 7.3.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains block scope and initialization-before-reference errors through execution examples.
Examples
Uses an early reference to x, a delayed function call, and nested blocks to demonstrate scope and initialization order.
What you can do after
Understand when a const binding is in scope and why referencing it before initialization throws a ReferenceError.
How it connects
Connects const with let, var, block scope, closures, hoisting, and the temporal dead zone.

Also explained in

Compare how 11 books explain constant →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.