JavaScript Interview Questions & Answers
1. What are the data types in JavaScript?
- Primitive: string, number, boolean, null, undefined, symbol, bigint
- Non-primitive: object, array, function
2. Difference between var, let, const?
- var: function scoped, hoisted, re-declarable
- let: block scoped, not hoisted, re-assignable
- const: block scoped, not hoisted, not re-assignable
3. What is hoisting?
- JavaScript moves declarations to the top of scope before code execution.
4. == vs ===
- == compares values with coercion; === compares values & types strictly.
5. Closures?
- A function that retains access to its outer scope even after execution.
6. Scope types?
- Global, function/local, block.
7. Array methods?
- map, filter, reduce, forEach, push, pop, shift, unshift, find, some, every.
8. Deep cloning?
- JSON.parse(JSON.stringify(obj)), or structuredClone(obj).
9. Arrow functions?
- Short syntax, no own this.
10. Destructuring?
- const {name} = obj; const [a, b] = arr;
11. Spread & Rest?
- Spread: [...arr]; Rest: function(...args) {}
12. Promise?
- Represents future result. States: pending, fulfilled, rejected.
13. async/await?
- Makes promises easier to read/write.
14. Event loop?
- Manages execution stack and async queue.
15. DOM Selection?
- getElementById, querySelector, querySelectorAll
16. Event delegation?
- Using parent to handle events on children.
17. Classes?
- ES6 syntax for creating objects using class and constructor.
18. Prototypal Inheritance?
- Objects inherit from other objects via __proto__.
19. Storage?
- localStorage, sessionStorage, cookies (diff in size, persistence, HTTP usage).
20. Debounce?
function debounce(fn, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
21. Flatten array?
function flatten(arr) {
return arr.flat(Infinity);