Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The const declaration creates a block-scoped, read-only reference to a value. It establishes an immutable binding, meaning the variable identifier cannot be reassigned to a new value, nor can it be redeclared within the same lexical scope.
const name1 = value1 [, name2 = value2 [, ...]];

Initialization Requirement

Because the binding cannot be altered after creation, a const declaration requires an immediate initializer. Declaring a const without an assignment throws a SyntaxError during the parsing phase.
const MAX_RETRIES = 5; // Valid
const TIMEOUT; // SyntaxError: Missing initializer in const declaration

Reassignment and Redeclaration

Attempting to reassign a const identifier throws a runtime TypeError.
const API_URL = "https://api.example.com";
API_URL = "https://api.v2.example.com"; // TypeError: Assignment to constant variable.
Attempting to redeclare an identifier that already exists in the same lexical scope throws a parse-time SyntaxError.
const MAX_USERS = 100;
const MAX_USERS = 200; // SyntaxError: Identifier 'MAX_USERS' has already been declared

Immutable Binding vs. Immutable Value

The const keyword guarantees an immutable binding, not an immutable value. If the assigned value is a reference type (such as an Object or an Array), the underlying data structure is not frozen. The properties or elements of that structure can be mutated, provided the variable identifier itself is not reassigned to a new memory address.
const config = { port: 8080 };
config.port = 3000; // Valid: Mutating the object's property

const ports = [80, 443];
ports.push(8080);   // Valid: Mutating the array
const config = { port: 8080 };
config = { port: 3000 }; // TypeError: Assignment to constant variable.

Block Scope and the Temporal Dead Zone (TDZ)

const declarations are block-scoped, meaning their visibility is limited to the nearest enclosing block { ... }.
{
  const limit = 50;
}
console.log(limit); // ReferenceError: limit is not defined
During execution context creation, const declarations are hoisted to the top of their block scope but remain uninitialized. The period between entering the scope and the actual lexical declaration is known as the Temporal Dead Zone (TDZ). Accessing the identifier within the TDZ throws a ReferenceError.
{
  // TDZ starts for 'threshold'
  console.log(threshold); // ReferenceError: Cannot access 'threshold' before initialization
  const threshold = 10;   // TDZ ends, binding is initialized
}

Global Object Context

When executed in a global script context (such as a standard <script> tag in a browser), const declarations do not create properties on the global object (e.g., window). This contrasts with var, which does append its identifier to the global environment record. (Note: In Node.js, top-level declarations are scoped to the module wrapper by default, meaning neither var nor const creates a property on the global object.)
// Executed in a browser's global script scope
var legacyVersion = "0.9.0";
const version = "1.0.0";

console.log(window.legacyVersion); // "0.9.0"
console.log(window.version);       // undefined
Master JavaScript with Deep Grasping Methodology!Learn More