Expert-Level JavaScript Interview Questions & Answers
Q: What is the difference between var, let and const in hoisting?
A: var is hoisted and initialized with undefined. let and const are hoisted but not initialized (TDZ applies).
Q: What is a closure?
A: A closure is a function that remembers its lexical scope even when executed outside of it.
Q: Explain the Event Loop in JavaScript.
A: The event loop allows JavaScript to perform non-blocking operations by putting callbacks in a queue and
processing them after the current execution.
Q: What are microtasks and macrotasks?
A: Microtasks include Promise callbacks. Macrotasks include setTimeout, setInterval, etc. Microtasks run
before macrotasks.
Q: How does Object.freeze differ from Object.seal and Object.preventExtensions?
A: freeze: no add/delete/change. seal: no add/delete. preventExtensions: no add, delete allowed.
Q: How does garbage collection work in JS?
A: JS uses mark-and-sweep algorithm to collect unreachable objects from memory.
Q: What is the output of `typeof null`? Why?
A: It is 'object' due to a legacy bug in JavaScript's implementation.
Q: Explain the use of Symbol in JavaScript.
A: Symbols are unique identifiers used to create non-enumerable object properties.
Q: What is currying in JS?
A: Transforming a function with multiple arguments into a sequence of functions each taking one argument.
Q: Implement debounce in JavaScript.
A: function debounce(fn, delay) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() =>
fn(...args), delay); }; }