π§ Mastering let, var, and const in JavaScript β Beginner to Advanced Guide with Real Interview Questions

Feature | var | let | const |
Scope | Function | Block | Block |
Hoisting | Yes (initialized to undefined ) | Yes (but not initialized β TDZ) | Yes (not initialized β TDZ) |
Re-declaration | β Allowed | β Not allowed | β Not allowed |
Re-assignment | β Allowed | β Allowed | β Not allowed (binding only) |
Attached to global obj | β Yes | β No | β No |
β οΈ 2. Temporal Dead Zone (TDZ)
Even though let
and const
are hoisted, they are not initialized. This period between hoisting and declaration is called the Temporal Dead Zone.
console.log(x); // β ReferenceError
let x = 10;
π 3. Loops and Closures
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
} // Outputs: 3, 3, 3
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
} // Outputs: 0, 1, 2
Why? Because let
creates a new block scope per iteration, var
doesnβt.
π¦ 4. const
and Object Mutability
const obj = { a: 1 };
obj.a = 2; // β
Allowed
obj = { b: 2 }; // β Error
const
prevents reassignment, not mutation.
β 5. When Should You Use Which?
Use
const
by default for stable referencesUse
let
for reassignable valuesAvoid
var
unless working with legacy code
π οΈ 6. Internal Memory Model
Hereβs what happens under the hood during the creation phase:
function run() {
console.log(a); // undefined
console.log(b); // ReferenceError
var a = 10;
let b = 20;
}
Behind the scenes:
var a
is hoisted and initialized toundefined
let b
is hoisted but uninitialized β TDZAccessing
b
before declaration causesReferenceError
π£ 7. Real World Pitfall
var token = "abc123";
if (true) {
var token = "newToken"; // Overwrites global token
}
console.log(token); // newToken (β security risk!)
Fix it with let
or const
:
let token = "abc123";
if (true) {
let token = "newToken"; // Scoped inside block
}
console.log(token); // abc123 β
π― Interview Questions with Answers
β Q1: What is hoisting?
A: JavaScript moves declarations to the top. var
is initialized to undefined
, let
/const
stay in the TDZ.
β Q2: Why prefer let
over var
?
A: let
is block-scoped and avoids accidental overwrites.
β Q3: Is const
immutable?
A: No, only the reference is constant. The object/array inside it can be mutated.
β Q4: Explain TDZ with an example.
{
// TDZ for x starts
console.log(x); // β ReferenceError
let x = 5;
}
β Q5: Difference in loops?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
var
: prints 3, 3, 3let
: prints 0, 1, 2 β
β Q6: Can you redeclare let
?
let x = 1;
let x = 2; // β SyntaxError
β Q7: Is const
more performant?
A: Not directly. But helps optimize JS engine assumptions and prevents accidental mutations.
π Summary Table
Criteria | Use var | Use let | Use const |
Reassign needed? | β | β | β |
Prevent re-declare? | β | β | β |
Scope needed | Function | Block | Block |
Mutation allowed | β | β | β (objects/arrays) |
Default recommendation | β | β | β β β |
π§ Final Advice
Use const
as your default. Only fall back to let
when reassignment is needed. Avoid var
unless youβre working on legacy codebases.
Subscribe to my newsletter
Read articles from krishan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

krishan
krishan
π Hey, I'm a Frontend Developer passionate about building clean, user-friendly interfaces. π Learning and sharing everything from React, JavaScript, HTML/CSS to advanced topics like Data Structures, Algorithms & System Design. π» Documenting my journey from fundamentals to becoming a top-tier developer β one blog at a time. π Join me as I break down complex topics into easy, practical lessons β with GitHub repos, code snippets, and real-world examples. π Consistent growth, community learning, and aiming high!