Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Block Scope

const creates a block-scoped binding that must be initialized immediately and cannot be reassigned. The referenced object or array may still be mutated.

Since
ES2015

How it works

A const declaration creates a binding available only within its containing block, function, or module. The binding must be initialized when declared, cannot be reassigned, and cannot be accessed before its declaration is reached because it is in the temporal dead zone. If it refers to an object or array, the binding stays fixed while the referenced contents may still change.

Syntax

const name = value;

Key points

  • A const binding is local to its surrounding block, function, or module rather than being generally visible throughout the program.
  • Assignment cannot replace the value held by a const binding after initialization.
  • Object and array contents remain mutable unless the referenced value is separately protected.
  • A top-level script const does not become a property of the global object.
  • const must appear in a declaration form; it cannot be used as a standalone statement body.

Common mistakes

  • const prevents reassignment of the binding, not mutation of an object or array referenced by that binding.
  • Reading a const before its declaration throws a ReferenceError; moving the declaration later does not make the earlier read valid.
  • A second declaration using the same name in one scope is an error, although a nested block may declare a shadowing binding.

Example: A block-local const shadows an outer binding

const name = "outer";
{
  const name = "inner";
  console.log(name); // inner
}
console.log(name); // outer

The inner declaration is limited to the block, so it shadows the outer binding only inside those braces.

When to use it

Fixed local configuration

Keep a binding from being reassigned after setup.

Block-local variables

Limit temporary names to a branch or loop body.

Mutable object state

Share a stable reference while updating object contents.

Interview questions

QHow does const differ from var regarding block scope?

const is block-scoped, so a binding declared inside braces is unavailable outside them. A nested declaration with the same name is a separate binding and can shadow an outer one.

Follow-up: What happens with var in the same block? var is not block-scoped; it belongs to the containing function or script instead.

QWhy must a const declaration be initialized immediately?

A const binding must receive an initializer in its declaration. It cannot later be assigned a value, because reassignment of the binding is disallowed.

Follow-up: Can const x; be made valid? No; unlike an uninitialized let, a const declaration without an initializer is invalid.

QWhat is the temporal dead zone for const?

The declaration is in the temporal dead zone from the beginning of its scope until initialization is processed. Accessing it during that interval throws a ReferenceError.

Follow-up: Does placing the declaration later permit an earlier read? No; the binding exists for scope analysis, but it is inaccessible until execution reaches initialization.

  • let
  • var
  • block statement
  • temporal dead zone
  • globalThis

Source: const - JavaScript | MDN Web Docs. Checked 2026-10-15.