π§ Why This Matters
This isnβt just about var, let, and const.
This is about:
Interviewers use scope, hoisting, and closure challenges to
separate people who know syntax from those who know how JavaScript really
works under the hood.
Letβs break it down β and then apply it in battle-tested,
real-world problems.
π Scope in JavaScript
Scope defines where variables and functions are
accessible.
π‘ Types of Scope:
|
Scope |
Description |
|
Global |
Declared outside any function |
|
Function |
Variables available only inside functions |
|
Block |
Declared with let or const inside {} |
|
Lexical |
Scope preserved based on code structure |
β
Scope Example:
function
run() {
let x = 5;
if (true) {
let y = 10;
console.log(x); // β
5
console.log(y); // β
10
}
console.log(y); // β ReferenceError
}
π Real-World Challenge
#1: Scope Shadowing
var
a = 1;
function
outer() {
var a = 2;
function inner() {
console.log(a);
}
return inner;
}
const
result = outer();
result();
// What gets logged?
β
Answer: 2
Because of lexical scope, inner() remembers the a defined in its outer
environment.
π Hoisting in JavaScript
π¨ Hoisting = Declarations
are moved up during compilation
console.log(name);
// undefined
var
name = "Ali";
console.log(age);
// β ReferenceError
let
age = 25;
var is hoisted (with value undefined)
let is hoisted but in the Temporal Dead Zone (TDZ) until initialized
π Real-World Challenge
#2: TDZ Trap
function
test() {
console.log(a); // ???
let a = 10;
}
test();
β
Answer: ReferenceError
Why? let was hoisted but not initialized β it's in the Temporal Dead Zone.
π Closures β Functions
That Remember
A closure is when a function βremembersβ variables
from its outer scope β even after that outer scope has finished executing.
π Closure Example
function
counter() {
let count = 0;
return function () {
return ++count;
};
}
const
increment = counter();
console.log(increment());
// 1
console.log(increment());
// 2
β
count stays in memory thanks to
the closure
π Real-World Challenge
#3: Custom Counter Using Closure
// Implement this function:
const
counter = createCounter();
console.log(counter());
// 1
console.log(counter());
// 2
console.log(counter());
// 3
β
Solution:
function
createCounter() {
let count = 0;
return function () {
return ++count;
};
}
π Closure in Loops β The
Classic JS Bug
for
(var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
//
Expected: 0 1 2
//
Actual: 3 3 3
β
Fix 1: Use let (block scoped):
for
(let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000); //
β
0 1 2
}
β
Fix 2: Use closure (IIFE):
for
(var i = 0; i < 3; i++) {
(function (x) {
setTimeout(() => console.log(x), 1000);
})(i);
}
π Real-World Challenge
#4: Closure in DOM Event Handlers<button id="btn0">Button 0</button>
<button
id="btn1">Button 1</button>
<button
id="btn2">Button 2</button>
for
(var i = 0; i < 3; i++) {
document.getElementById(`btn${i}`).addEventListener("click",
function () {
console.log(i); // β Always logs 3
});
}
β
Fix it using a closure:
for
(var i = 0; i < 3; i++) {
(function (index) {
document.getElementById(`btn${index}`).addEventListener("click",
function () {
console.log(index);
});
})(i);
}
β
Or just use let:
for
(let i = 0; i < 3; i++) {
document.getElementById(`btn${i}`).addEventListener("click",
function () {
console.log(i);
});
}
β
Summary
|
Concept |
Key
Takeaways |
|
Scope |
Determines
variable visibility and lifespan |
|
Hoisting |
Variables/functions
declared early but initialized later |
|
TDZ |
Accessing
let/const before initialization throws error |
|
Closures |
Functions
keep memory of outer variables after execution |
|
Real
Usage |
Used
in counters, timers, event handlers, private variables |
Focus on the fundamentals: closures, scoping, async/await, and the event loop. Practice real code challenges, not just theory.
Mostly yes, but you also need to understand JavaScriptβs core behaviors beyond just syntax β like prototype chains and hoisting.
Because they reveal whether a developer understands how memory, function environments, and execution context work in JS.
Yes. Even full-stack roles often involve Node.js questions, but core JS concepts are always in play.
Very. Interviewers often test your ability to handle real-world async flows β promises, async/await, and the event loop.
Practice! Understand the why and how behind behaviors β interviews often involve debugging or refactoring.
Yes β often in coding challenges or refactor tasks to test clean, functional thinking.
Absolutely. You should understand how prototypes work and how classes build on top of them.
Platforms like LeetCode, CodeSignal, JSFiddle, and browser DevTools are great. Pair with actual coding in VS Code for muscle memory.
Rushing to code without fully understanding the problem. Many JS bugs come from misunderstanding scope, context, or async behavior.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. Weβll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)