Each question combines 2-4 Level 1 concepts to simulate real-world complexity and interview scenarios.
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}What will be logged and why?
- Tests: Hoisting, Scopes, Closures, Event Loop
- Answer: All logs will show
3due to closure capturing the reference toi, not the value
for (let i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}What will be logged and why?
- Tests: Block scope, Temporal Dead Zone, Closures, Event Loop
- Answer:
0, 1, 2becauseletcreates block scope for each iteration
var x = 10;
function outer() {
var x = 20;
function inner() {
console.log(x);
}
return inner;
}
var fn = outer();
fn();What will be logged and why?
- Tests: Hoisting, Scopes, Closures, Lexical scoping
- Answer:
20due to lexical scoping and closure capturing thexfromouter's scope
var name = "Global";
var obj = {
name: "Object",
getName: function() {
return function() {
return this.name;
};
}
};
console.log(obj.getName()());What will be logged and why?
- Tests: This keyword, Context loss, Closures, Global scope
- Answer:
"Global"because the returned function loses itsthiscontext and defaults to global
var name = "Global";
var obj = {
name: "Object",
getName: function() {
return () => {
return this.name;
};
}
};
console.log(obj.getName()());What will be logged and why?
- Tests: Arrow functions, This binding, Closures, Lexical scoping
- Answer:
"Object"because arrow functions preserve thethiscontext from their enclosing scope
var name = "Global";
var obj = {
name: "Object",
getName: function() {
return this.name;
}
};
var getNameFn = obj.getName;
console.log(getNameFn());What will be logged and why?
- Tests: This keyword, Context loss, Method context, Global scope
- Answer:
"Global"becausegetNameFnis called in global context, losing the object context
for (var i = 0; i < 3; i++) {
(function(j) {
setTimeout(function() {
console.log(j);
}, j * 1000);
})(i);
}What will be logged and when?
- Tests: Event Loop, Closures, IIFE, Scopes, setTimeout
- Answer:
0immediately,1after 1 second,2after 2 seconds due to IIFE creating new scope for each iteration
console.log('1');
setTimeout(() => {
console.log('2');
}, 0);
Promise.resolve().then(() => {
console.log('3');
});
console.log('4');What will be the output order and why?
- Tests: Event Loop, Microtasks, Macrotasks, Call Stack
- Answer:
1, 4, 3, 2because microtasks (Promises) have priority over macrotasks (setTimeout)
var name = "Global";
var counter = 0;
function createCounter() {
var counter = 0;
return {
increment: function() {
counter++;
return counter;
},
getValue: function() {
return counter;
}
};
}
var counter1 = createCounter();
var counter2 = createCounter();
console.log(counter1.increment()); // ?
console.log(counter1.increment()); // ?
console.log(counter2.increment()); // ?
console.log(counter1.getValue()); // ?
console.log(counter2.getValue()); // ?What will be logged and why?
- Tests: ALL Level 1 concepts combined
- Answer:
1, 2, 1, 2, 1due to closures creating separate counter variables for each instance
var x = 1;
function outer() {
var x = 2;
function inner() {
var x = 3;
setTimeout(function() {
console.log(x);
}, 1000);
}
inner();
}
outer();What will be logged and why?
- Tests: Scope chain, Closures, Event Loop, Variable shadowing
- Answer:
3because the closure captures thexfrom theinnerfunction's scope
- Given code, predict the output
- Explain the reasoning step by step
- Identify which concepts are at play
- Find bugs in given code
- Explain why they occur
- Provide solutions
- Write code that demonstrates specific concept interactions
- Implement solutions to common problems
- Show understanding of best practices
- Explain how multiple concepts work together
- Describe real-world scenarios
- Discuss trade-offs and alternatives
- Start Simple: Begin with questions combining 2 concepts
- Build Complexity: Gradually add more concepts
- Time Yourself: Practice under interview conditions
- Explain Aloud: Verbalize your reasoning process
- Draw Diagrams: Visualize scope chains and execution flow
- Multiple Solutions: Find different ways to solve the same problem