Facebook Pixel

var vs let vs const Hoisting in JavaScript

All three are hoisted, but they behave very differently. Here is exactly how each one is hoisted.

var vs let vs const Hoisting in JavaScript

All three declarations are hoisted in JavaScript, meaning memory is allocated for them before code execution. But they behave very differently.

var: Hoisted and Initialized to undefined

console.log(x); // undefined var x = 10;

The declaration is hoisted, the variable is initialized to undefined, and the assignment happens at the line. No error, just undefined until the line runs.

let and const: Hoisted but Uninitialized (TDZ)

console.log(y); // ReferenceError: Cannot access 'y' before initialization let y = 10;

Memory is allocated for y, but the variable stays in the Temporal Dead Zone until the declaration line. Accessing it before that line throws a ReferenceError.

Why the Difference?

var is from pre-ES6 JavaScript. let and const were introduced in ES6 to prevent bugs caused by accessing variables too early. The TDZ is a safety feature: it forces you to declare before use.

const Has an Extra Rule

const must be initialized at the declaration line:

const z; // SyntaxError: Missing initializer const z = 5; // OK

And it cannot be reassigned after:

const z = 5; z = 10; // TypeError: Assignment to constant variable

Note: const does not make objects immutable. You can still mutate properties of a const object.

Block Scope vs Function Scope

  • var is function-scoped (or global if declared outside a function).
  • let and const are block-scoped (a block is anything inside {}).
if (true) { var a = 1; let b = 2; } console.log(a); // 1 console.log(b); // ReferenceError

Re-declaration

  • var allows re-declaration in the same scope (silent, buggy).
  • let and const throw a SyntaxError on re-declaration in the same scope.

The Takeaway

All three are hoisted, but var is initialized to undefined while let and const sit in the TDZ. Use const by default, let when you need to reassign, and avoid var in modern code.

var is hoisted and initialized to undefined. let and const are hoisted (memory allocated) but kept uninitialized in the Temporal Dead Zone. Accessing them before the declaration line throws a ReferenceError.

Yes, memory is allocated for them. But they remain in the Temporal Dead Zone until the declaration line is reached. Accessing them early throws a ReferenceError, unlike var which returns undefined.

The period between the start of a block scope and the line where a let or const variable is declared. During this period, accessing the variable throws a ReferenceError.

No. Redeclaring let or const in the same scope throws a SyntaxError. var allows redeclaration silently, which is one reason to avoid var in modern code.

No. const prevents reassignment of the variable, but you can still mutate the object's properties. Use Object.freeze() if you want true immutability.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.