Top JavaScript Interview Questions and Answers
The JavaScript interview questions asked most often, with clear answers and code examples covering scope, closures, this, promises and the event loop.
JavaScript fundamentals come up in almost every frontend and full-stack interview. These are the questions interviewers ask most, with short answers you can explain out loud and code you can write on a whiteboard.
1. What is the difference between var, let and const?#
var is function-scoped and can be redeclared. let and const are block-scoped. const cannot be reassigned, but the object or array it holds can still be changed.
if (true) {
var a = 1;
let b = 2;
}
console.log(a); // 1
console.log(b); // ReferenceError: b is not defined
const user = { name: "Asha" };
user.name = "Ravi"; // allowed
user = {}; // TypeError: Assignment to constant variable2. What is hoisting?#
Before code runs, JavaScript moves declarations to the top of their scope. var variables are hoisted and set to undefined. let and const are hoisted too, but stay in the temporal dead zone until their line runs, so accessing them early throws an error. Function declarations are hoisted completely.
console.log(x); // undefined
var x = 5;
console.log(y); // ReferenceError
let y = 5;
greet(); // works
function greet() { console.log("Hi"); }3. What is a closure?#
A closure is a function that remembers variables from the scope where it was created, even after that outer function has finished. Closures are used for private data, function factories and callbacks.
function createCounter() {
let count = 0;
return () => ++count;
}
const counter = createCounter();
counter(); // 1
counter(); // 24. What is the difference between == and ===?#
== converts both values to the same type before comparing (type coercion). === compares both value and type with no conversion. Use === by default.
0 == "0"; // true
0 === "0"; // false
null == undefined; // true
null === undefined; // false5. How does "this" work?#
The value of this depends on how a function is called, not where it is written:
- As an object method:
thisis the object. - As a plain function:
undefinedin strict mode, the global object otherwise. - With
call,applyorbind: whatever you pass in. - With
new: the newly created object. - Arrow functions have no own
this. They usethisfrom the surrounding scope.
6. Explain the event loop#
JavaScript runs on a single thread. Synchronous code runs first on the call stack. When the stack is empty, the event loop runs all microtasks (promise callbacks) and then the next macrotask (like setTimeout).
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// Output: A, D, C, BThis output question is extremely common. Explain why C prints before B: microtasks always run before the next macrotask.
7. What is the difference between Promise.all and Promise.allSettled?#
Promise.all rejects as soon as any promise rejects. Promise.allSettled waits for every promise and returns each result with a status of "fulfilled" or "rejected". Use allSettled when one failure shouldn't cancel the rest.
8. What are debounce and throttle?#
Debounce waits until calls stop for a set time, then runs once. It suits search boxes. Throttle runs at most once per time interval. It suits scroll and resize handlers.
function debounce(fn, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}9. What is the difference between shallow and deep copy?#
A shallow copy copies only the top level, so nested objects are still shared. A deep copy copies everything. structuredClone() is the modern built-in way to deep copy.
const original = { a: 1, nested: { b: 2 } };
const shallow = { ...original };
const deep = structuredClone(original);
shallow.nested.b = 99;
console.log(original.nested.b); // 99 (shared)
console.log(deep.nested.b); // 2Saying JSON.parse(JSON.stringify(obj)) is a perfect deep copy. It loses functions, undefined, dates and Map/Set values.
Key takeaways#
- Know scope, hoisting and closures well. Many other questions build on them.
- Be ready to predict event loop output and explain microtasks vs macrotasks.
- Write debounce from memory. It's a favourite live-coding task.