JavaScript Interview Questions
10 questions with detailed answers
Q1. What is closure in JavaScript?
Answer: A closure is a function that retains access to its outer scope variables even after the outer function has returned.
Q2. Difference between var, let, and const?
Answer: var is function-scoped and hoisted; let and const are block-scoped. const cannot be reassigned.
Q3. What is the event loop?
Answer: JavaScript's concurrency model: call stack, callback queue, and microtask queue manage async operations.
Q4. What is hoisting?
Answer: Variable and function declarations are moved to the top of their scope during compilation.
Q5. Explain prototypal inheritance.
Answer: Objects inherit properties from their prototype chain via __proto__ or Object.getPrototypeOf().
Q6. What is the difference between == and ===?
Answer: == performs type coercion before comparison, while === compares both type and value without coercion. Strict equality is generally safer and more predictable.
Q7. How do Promise.all and Promise.allSettled differ?
Answer: Promise.all resolves when every promise resolves but rejects immediately when one fails. Promise.allSettled always waits for every promise and returns each fulfilled or rejected result.
Q8. What is event delegation?
Answer: Event delegation attaches one listener to a common ancestor and uses event bubbling to handle child interactions. It reduces listeners and also works for dynamically added children.
Q9. What do call, apply, and bind do?
Answer: call invokes a function with a chosen this value and separate arguments; apply does the same with an argument array; bind returns a new function with this and optional arguments fixed.
Q10. What is the difference between null and undefined?
Answer: undefined usually means a value was not assigned or is missing. null is an explicit value representing intentional absence. They are loosely equal but not strictly equal.