Declarations, hoisting, and this

ECMAScript 2026

See what happens when code reads a name before its declaration, then compare how two kinds of functions get this.

On this page

var is available before its initializer

A var declaration belongs to its enclosing function. JavaScript creates its binding and initializes it to undefined before running the function body. The assignment still happens where the line runs.

How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.

function order() {
  console.log(quantity);
  var quantity = 2;
  console.log(quantity);
}
order();

What to expect The console prints undefined, then 2.

let and the temporal dead zone

let belongs to its block. The name exists from the start of the block, but reading it before its declaration executes throws a ReferenceError. That interval is called the temporal dead zone.

How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.

{
  try {
    console.log(quantity);
  } catch (error) {
    console.log(error.name);
  }
  let quantity = 2;
  console.log(quantity);
}

What to expect The caught error prints ReferenceError, then the initialized value prints 2.

const fixes the binding

const is block-scoped and has the same temporal dead zone rule as let. It needs an initial value. It prevents reassigning the variable, while properties of its object can still change.

How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.

const order = { quantity: 2 };
order.quantity += 1;
console.log(order.quantity);

What to expect The object’s count becomes 3.

Arrow functions and this

In a regular method called as shop.receipt(), this is shop. An arrow function has no this of its own. The arrow inside receipt inherits this from the surrounding method and can read shop.name.

How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.

const shop = {
  name: "Example Store",
  receipt() {
    const print = () => console.log(this.name);
    print();
  }
};
shop.receipt();

What to expect The console prints Example Store. Change the shop name and rerun.

Official references