Most Commonly Asked JavaScript Interview Questions: Understanding JavaScript's Event Loop, Promises, setTimeOut, Micro and Macro Task Queues
Uvesh Chamadiya
SWE @Nuvolo(Client) | Software Engineer | DevOps Engineer | MERN Stack
JavaScript’s event loop, Micro and Macro Task Queues, promises, and setTimeout are frequent topics in technical interviews. Mastering these concepts not only helps in interviews but also in writing efficient JavaScript code. Here, we'll explore some commonly asked interview questions and provide detailed explanations for each.
Check out my detailed YouTube video on this topic here. In this video, I explain JavaScript event loop, Micro and Macro Task Queues, promises, and setTimeout with additional examples and visuals.
Question No. 1
Code:
console.log("Start");
Promise.resolve().then(() => {
console.log("1: Promise");
});
Promise.resolve().then(() => {
console.log("2: Promise");
});
console.log("Middle");
Promise.resolve().then(() => {
console.log("3: Promise");
});
Promise.resolve().then(() => {
console.log("4: Promise");
});
console.log("End");
Output:
Start
Middle
End
1: Promise
2: Promise
3: Promise
4: Promise
Explanation:
Question No. 2
Code:
console.log("Start");
Promise.resolve().then(() => {
console.log("1: Promise");
});
Promise.resolve().then(() => {
console.log("2: Promise");
// Nested Promise
Promise.resolve().then(() => {
console.log("3: Promise");
});
});
console.log("Middle");
Promise.resolve().then(() => {
console.log("4: Promise");
});
console.log("End");
Output:
Start
Middle
End
1: Promise
2: Promise
4: Promise
3: Promise
Explanation:
Question No. 3
Code:
领英推荐
console.log("Start");
setTimeout(() => {
console.log("1: setTimeout");
}, 100);
setTimeout(() => {
console.log("2: setTimeout");
}, 0);
console.log("End");
Output:
Start
End
2: setTimeout
1: setTimeout
Explanation:
Question No. 4
Code:
console.log("Start");
setTimeout(() => {
console.log("1: setTimeout");
}, 0);
setTimeout(() => {
console.log("2: setTimeout");
setTimeout(() => {
console.log("3: setTimeout");
}, 0);
}, 0);
console.log("Middle");
setTimeout(() => {
console.log("4: setTimeout");
}, 0);
console.log("End");
Output:
Start
Middle
End
1: setTimeout
2: setTimeout
4: setTimeout
3: setTimeout
Explanation:
Question No. 5
Code:
console.log("Start");
setTimeout(() => {
console.log("Timeout 1");
}, 100);
Promise.resolve().then(() => {
console.log("Promise 1");
setTimeout(() => {
console.log("Timeout 2");
}, 0);
Promise.resolve().then(() => {
console.log("Promise 2");
});
});
setTimeout(() => {
console.log("Timeout 3");
}, 0);
Promise.resolve().then(() => {
console.log("Promise 3");
});
console.log("End");
Output:
Start
End
Promise 1
Promise 3
Promise 2
Timeout 3
Timeout 2
Timeout 1
Explanation:
Understanding the event loop, Micro and Macro Task Queues, promises, and setTimeout is crucial for mastering JavaScript. These questions help clarify how JavaScript handles asynchronous operations and the order in which tasks are executed.
Teacher & CTO at Vidhya Skill School | 17 out of 39 hackathons winner | Built system for fastag(NPCI), HDFC, & 42+ companies | Mentoring Next-Gen engineers | Offering live interactive classes | Fixing Unemployment
7 个月Understanding the in depth and internal concepts is always helpful as it helps us to understand the working principles of anything.