Fixed local configuration
Keep a binding from being reassigned after setup.
JS Puzzles · Const
const creates a block-scoped binding that must be initialized immediately and cannot be reassigned. The referenced object or array may still be mutated.
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.
const name = value;const binding is local to its surrounding block, function, or module rather than being generally visible throughout the program.const binding after initialization.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.const prevents reassignment of the binding, not mutation of an object or array referenced by that binding.const before its declaration throws a ReferenceError; moving the declaration later does not make the earlier read valid.const name = "outer";
{
const name = "inner";
console.log(name); // inner
}
console.log(name); // outerThe inner declaration is limited to the block, so it shadows the outer binding only inside those braces.
Keep a binding from being reassigned after setup.
Limit temporary names to a branch or loop body.
Share a stable reference while updating object contents.
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.
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.
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.
letvarblock statementtemporal dead zoneglobalThis