This chapter follows JavaScript syntax at a glance.
var, let, and const
var— function-scoped (or global-scoped at top level). Hoisted and initialized toundefinedbefore the line runs.let— block-scoped. Hoisted but not initialized until the declaration line (temporal dead zone).const— block-scoped likelet; must have an initializer; rebinding the name is forbidden.
var a = 1;let b = 2;const c = 3;
// let b = 4; // SyntaxError: already declared// c = 5; // TypeError: assignment to constantUse const by default, let when reassignment is required, and avoid new var in modern code unless you are mirroring legacy patterns.
Hoisting
Hoisting means the engine registers bindings during the compile phase of the scope before execution. var declarations behave as if moved to the top of the function with value undefined. let and const are hoisted but inaccessible until their declaration is evaluated.
console.log(typeof undeclared); // ReferenceErrorconsole.log(typeof hoistedVar); // "undefined" (var exists, not yet assigned)var hoistedVar = 10;
console.log(typeof hoistedLet); // ReferenceError (TDZ)let hoistedLet = 20;Function declarations are fully hoisted (name and body); function expressions bound with var follow var rules.
Temporal dead zone
From the start of the block until the let or const line, the binding exists but reading or writing it throws ReferenceError. That closes the hole that var left in loops and switch cases.
{ // TDZ for `value` starts here // console.log(value); let value = 1; // TDZ ends}The TDZ also applies to const and to the left-hand side of a let/const that references itself in the initializer (const x = x throws).
const and mutability
const prevents reassignment of the binding, not mutation of a referenced object or array.
const user = { name: "Ada" };user.name = "Grace"; // allowed// user = {}; // TypeError
const nums = [1, 2];nums.push(3); // allowedFor deep immutability you need patterns such as Object.freeze (shallow), immutable data libraries, or copying before update—JavaScript does not enforce const-correctness at the type level without TypeScript or similar.
Global object and modules
In browsers, top-level var in a classic script creates a property on window (unless shadowed). In modules, top-level bindings are module-scoped and are not properties of globalThis.
var legacy = 1;// In a script: globalThis.legacy === 1// In a module: legacy is not on globalThisglobalThis is the standard name for the global object in any host (browser, Node, workers).
Redeclaration rules
var can be redeclared in the same function scope. let and const cannot be redeclared in the same scope. let and const in different blocks can reuse names only when scopes do not overlap confusingly—linters usually forbid shadowing.
Next: Operators in JavaScript.