Skip to content
UniCircle
GuideEngineeringB.Tech / B.E., BCA, MCA

var, let and const in JavaScript, and what hoisting really means

The three ways to declare a variable, how their scope differs, what the temporal dead zone is, and a simple rule for which one to use.

By UniCircle team2 min readPublished 22 Oct 2026Undergraduate

Scope: the main difference

  • var is scoped to the whole function (or the whole script). A var declared inside a loop or an if block is visible outside it.
  • let and const are scoped to the nearest pair of curly braces.
  • const cannot be reassigned. The value it points to can still change if it is an object or an array.
JavaScript
function demo() {
  if (true) { var a = 1; let b = 2; }
  console.log(typeof a, typeof b);
  const marks = [70, 80];
  marks.push(90);
  console.log(marks.length);
}
demo();
text
number undefined
3

Hoisting, in plain words

Before code runs, JavaScript sets up every declaration in its scope. A var is created early and set to undefined. A let or const is also created early, but you cannot touch it until its line runs. That gap is the temporal dead zone.

JavaScript
console.log(x);
var x = 5;
try { console.log(y); let y = 6; } catch (e) { console.log(e.name); }
console.log(greet());
function greet() { return 'function declarations are hoisted with their body'; }
text
undefined
ReferenceError
function declarations are hoisted with their body

The classic loop question

JavaScript
const fns = [];
for (var i = 0; i < 3; i++) fns.push(() => i);
const lets = [];
for (let j = 0; j < 3; j++) lets.push(() => j);
console.log(fns.map(f => f()).join(','), lets.map(f => f()).join(','));
text
3,3,3 0,1,2

With var there is one i shared by every function, and it is 3 by the time they run. With let, each loop turn gets its own j.

A rule you can follow

Use const by default, let when you really need to reassign, and avoid var in new code. Practise more in UniCircle interview prep: JavaScript and TypeScript.

#javascript#let#const#hoisting

Sources and further reading

Practise this as an interview question

Written by the UniCircle team, checked by an editor on 22 Oct 2026. It is not official policy of any university. Exam patterns and rules change; confirm dates and rules on the official website.

Keep reading