Ace Your Coding Interview: JavaScript Q&A for Intermediate Learners
1. What are closures in JavaScript?
A closure is when a function can remember and use variables from its outer scope, even after that scope has finished.
function counter() {
let count = 0;
return function() {
count++;
return count;
};
}
const add = counter();
console.log(add()); // 1
console.log(add()); // 2
2. Difference between call()
, apply()
, and bind()
?
- call() → calls the function immediately, pass arguments one by one.
- apply() → same as call, but pass arguments as an array.
- bind() → returns a new function with
this
fixed, doesn’t run immediately.
3. Explain the event loop.
The event loop makes JavaScript handle tasks in order:
- Run all normal code first (synchronous).
- Then handle microtasks (like Promises).
- Then handle macrotasks (like
setTimeout
).
4. Difference between synchronous and asynchronous code?
- Synchronous: runs step by step, one after another.
- Asynchronous: doesn’t block; code runs in background (like API calls, setTimeout).
5. What is Promise.all()
vs Promise.race()
?
- Promise.all() → waits for all promises to finish (resolves if all succeed).
- Promise.race() → gives the result of the first promise that finishes.
6. Shallow copy vs deep copy?
- Shallow copy: top-level values are copied, but nested objects are still linked.
- Deep copy: creates a complete, independent clone.
7. What is this
in JavaScript?
- In global →
window
(in browser). - Inside an object method → that object.
- Inside arrow function → comes from outer scope (doesn’t create its own).
8. Difference between map()
, filter()
, reduce()
?
- map() → transforms every element.
- filter() → picks elements that match condition.
- reduce() → combines all into a single value.
9. What are arrow functions?
Shorter syntax for writing functions. They don’t have their own this
; they use this
from outer scope.
10. Difference between for...in
and for...of
?
- for...in → goes through object keys.
- for...of → goes through iterable values (like arrays).
11. What is prototypal inheritance?
Objects can inherit properties and methods from another object (its prototype).
12. What are IIFEs?
Immediately Invoked Function Expressions. Functions that run as soon as they’re defined.
13. What are higher-order functions?
Functions that take other functions as arguments, or return a function.
14. What is debouncing vs throttling?
- Debounce → runs only after the user stops triggering it (useful for search input).
- Throttle → makes sure a function runs at most once in a given time (useful for scroll events).
15. How do you remove duplicates from an array?
By using a Set:
let arr = [1,2,2,3];
let unique = [...new Set(arr)]; // [1,2,3]
16. Difference between localStorage, sessionStorage, and cookies?
- localStorage → saved permanently in the browser until cleared.
- sessionStorage → only for that browser tab session.
- cookies → small data stored, also sent with every request to the server.
17. What is a JavaScript module?
A module is reusable code that can be imported/exported across files using import
and export
.
18. Difference between null, undefined, and NaN?
- null → intentional empty value.
- undefined → variable declared but no value assigned.
- NaN → result of invalid math (Not-a-Number).
19. How do async/await work?
They are a cleaner way to work with Promises. Code looks synchronous, but actually runs asynchronously.
20. What are generators?
Special functions that can pause and resume using yield
. They return an iterator object.
21. What is memoization?
Storing the result of expensive function calls so you don’t have to calculate again if the same input repeats.
22. setTimeout
vs setInterval
?
- setTimeout → runs code once after given time.
- setInterval → runs code repeatedly after each interval.
23. Difference between Object.freeze()
and Object.seal()
?
- freeze() → you can’t change, add, or delete properties.
- seal() → you can update values, but can’t add or remove properties.
24. What is event delegation?
Instead of adding event listeners to many child elements, you add it once to a parent, and check the target inside.
25. What are WeakMap and WeakSet?
They are like Map and Set but hold weak references to objects. If the object is no longer used, it gets garbage-collected automatically. Useful for caching or private data.