Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 07 / 31FLOW AND SCOPE

Bindings and blocks.

Names live in scopes, and const protects a binding rather than an object.

01 / THE BASICS

Start here.

Names make values reusable. const total = 4; creates a binding named total, and a later expression can read it. let total = 4; also creates a binding but permits total = 5; later. The equals sign assigns a new value to an existing let binding when used without let. A name must be declared before ordinary code can use it; otherwise JavaScript cannot know which value you mean.

Scope is the part of a program where a name is available. Braces create a block for let and const. A name declared inside an if block cannot normally be read outside it. A nested block may declare a new binding with the same spelling as an outer one; this is shadowing. The inner binding temporarily hides the outer name, but it does not replace the outer value. Choosing distinct names is usually easier for a beginner to follow.

const protects the binding, not every object it points to. If settings is a const object, settings.theme = 'dark' changes one property of that object without reassigning settings. By contrast, settings = {} tries to make the name point to a new object and fails. Older JavaScript also has var, whose function-level scope behaves differently; learning let and const first gives a more predictable model.

02 / THE IDEA

Build the model.

A binding gives a value a name. let and const are scoped to the nearest block. A const binding cannot be reassigned, but the object it names may still change. This makes the binding stable without making its contents immutable.

Prefer const when the name should keep referring to the same value; use let when reassignment is part of the model. var has function scope and older hoisting behavior, so modern code usually avoids it. Shadowing a name in an inner block creates a new binding rather than editing the outer one.

03 / UNDER THE SURFACE

How it works.

A block is the region between braces. let and const declared inside it can be used there, but not outside it. This lets a branch or loop keep temporary names close to the work that needs them. A nested block may declare a name already used outside; that is a new binding called shadowing, so it can make code harder to read if overused.

const prevents assigning a different value to the binding. It does not freeze an object reached through the binding. If you need to prevent property changes, that is a separate decision; Object.freeze is shallow and has its own limits. Use let for a value that must be reassigned, such as a running total, and const for names whose association should remain stable.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const settings = { theme: 'light' };
settings.theme = 'dark'; // allowed
// settings = {} would fail

First notice. The object changed; the binding still points to that object.

settings receives one object. settings.theme = 'dark' follows the stable binding to that object and changes a property, so it succeeds. The commented-out settings = {} would try to point the const binding at a new object, so it would fail. The distinction is between changing the referenced object and reassigning the name.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
let count = 1;
{
  let count = 2;
  console.log(count);
}
console.log(count);

The block prints 2, and the final line prints 1. The inner let creates a separate binding that shadows the outer count until the block ends. If the inner let were removed, the assignment would affect the outer binding instead.

06 / KEEP THIS

One thing to remember.

Scope controls where a name is visible. const controls reassignment of that name.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.