Top JavaScript Interview Questions
Here are the most commonly asked JavaScript interview questions with detailed answers. Practice these to ace your next interview.
💡 Interview Tip
Don't just memorize answers — understand the underlying concepts. Interviewers often ask follow-up questions to test your depth of understanding.
Question 1: What is the difference between let, const, and var?
Answer:
var— Function-scoped, hoisted, can be redeclared. Avoid in modern code.let— Block-scoped, hoisted but in Temporal Dead Zone (TDZ), cannot be redeclared in same scope.const— Block-scoped, hoisted but in TDZ, cannot be reassigned. Use by default.
var x = 1; // function-scoped
let y = 2; // block-scoped
const z = 3; // block-scoped, cannot reassign
Question 2: What is the difference between == and ===?
Answer:
==— Loose equality. Performs type coercion before comparing.===— Strict equality. Compares both value and type. Always use this.
5 == '5' // true (type coercion)
5 === '5' // false (different types)
0 == false // true
0 === false // false
Question 3: What is a closure?
Answer:
A closure is a function that remembers its lexical scope even when executed outside that scope. Every function in JavaScript is a closure.
function outer() {
let message = "Hello";
function inner() {
console.log(message); // remembers 'message'
}
return inner;
}
const fn = outer();
fn(); // "Hello"
Question 4: What is the event loop?
Answer:
The event loop is a mechanism that handles asynchronous operations in JavaScript. It continuously checks if the call stack is empty and moves tasks from the task queue and microtask queue to the call stack.
- Microtasks (Promises) have higher priority
- Macrotasks (setTimeout, events) run after microtasks
Question 5: What is the difference between null and undefined?
Answer:
undefined— A variable that has been declared but not assigned a value. Type:"undefined"null— An intentional absence of value. Type:"object"(historical bug)
let x; // undefined
let y = null; // null
console.log(typeof null); // "object"
More Interview Questions
Practice these additional questions:
- What is hoisting?
- Explain
thisin JavaScript - What are Promises and how do they work?
- Explain async/await
- What is event delegation?
- What is the spread operator?
- Explain
map(),filter(), andreduce() - What is the difference between
call(),apply(), andbind()?
Quick Quiz
1. What is the result of typeof null?
2. Which of these is block-scoped?
3. What does 5 === '5' return?
4. What is a closure?
5. Which runs first: Promise or setTimeout?
Summary
varis function-scoped;letandconstare block-scoped- Always use
===over==for strict equality - A closure is a function that remembers its lexical scope
- The event loop handles async operations with microtasks (Promises) and macrotasks (setTimeout)
undefinedmeans "no value assigned";nullmeans "intentionally empty"typeof null === "object"is a historical bug
