Back to Blog
Career2025-03-05

JavaScript Interview Questions: Top 20 You Must Know

Prepare for your JavaScript interview with these 20 essential questions and detailed answers.

1. What is the difference between == and ===?

== performs type coercion before comparison, while === checks both value and type without coercion.

``javascript console.log(1 == '1'); // true (type coercion) console.log(1 === '1'); // false (strict equality) console.log(null == undefined); // true console.log(null === undefined); // false `

Best Practice: Always use === to avoid unexpected type coercion bugs.

2. Explain closures in JavaScript

A closure is a function that has access to variables from its outer (enclosing) function scope, even after the outer function has returned.

`javascript function createCounter() { let count = 0; return { increment: () => ++count, getCount: () => count }; }

const counter = createCounter(); counter.increment(); counter.increment(); console.log(counter.getCount()); // 2 `

3. What is the event loop?

The event loop is JavaScript's mechanism for handling asynchronous operations. It continuously checks the call stack and task queues, moving callbacks from the queue to the stack when the stack is empty.

`javascript console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4'); // Output: 1, 4, 3, 2 `

Microtasks (Promises) run before macrotasks (setTimeout).

4. Explain this keyword in JavaScript

this refers to the execution context. Its value depends on how a function is called:

`javascript const obj = { name: 'Alice', greet() { return this.name; }, // 'Alice' (method call) greetArrow: () => this.name // undefined (arrow inherits outer this) };

function standalone() { return this; } standalone(); // window (or undefined in strict mode) new standalone(); // new object standalone.call(obj); // obj `

5. What is the difference between var, let, and const?

- var: function-scoped, hoisted, can be redeclared - let: block-scoped, not hoisted (TDZ), can be reassigned - const: block-scoped, not hoisted (TDZ), cannot be reassigned

`javascript for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 100); // 3, 3, 3 } for (let j = 0; j < 3; j++) { setTimeout(() => console.log(j), 100); // 0, 1, 2 } `

6. What is hoisting?

JavaScript moves declarations to the top of their scope before execution. Function declarations are fully hoisted; var declarations are hoisted but not initialized.

`javascript console.log(x); // undefined (var is hoisted) var x = 5;

console.log(y); // ReferenceError (let has TDZ) let y = 10;

hello(); // Works! (function declaration hoisted) function hello() { console.log('hi'); } `

7. Explain prototype inheritance

Every JavaScript object has a prototype chain. When you access a property, JavaScript looks up the chain until it finds the property or reaches null.

`javascript function Animal(name) { this.name = name; } Animal.prototype.speak = function() { return this.name + ' makes a sound'; };

const dog = new Animal('Rex'); console.log(dog.speak()); // 'Rex makes a sound' console.log(dog.hasOwnProperty('name')); // true console.log(dog.hasOwnProperty('speak')); // false (on prototype) `

8. What are Promises?

Promises represent eventual async results. They can be pending, fulfilled, or rejected.

9. Explain the spread and rest operators

`javascript // Spread: expands elements const arr = [1, 2, 3]; const newArr = [...arr, 4, 5]; // [1, 2, 3, 4, 5] const obj = { a: 1 }; const newObj = { ...obj, b: 2 }; // { a: 1, b: 2 }

// Rest: collects elements function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); } sum(1, 2, 3, 4); // 10 `

10. What is debouncing and throttling?

`javascript // Debounce: wait until user stops triggering function debounce(fn, delay) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), delay); }; }

// Throttle: limit to once per interval function throttle(fn, limit) { let inThrottle; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; } `

11-20 Quick Summary

11. Map vs Object: Map has any key type, maintains insertion order, better for frequent additions/deletions.

12. Deep vs Shallow Copy: structuredClone() for deep copy, spread for shallow.

13. Event Delegation: Attach one listener to a parent instead of many to children.

14. async/await: Syntactic sugar over Promises for cleaner async code.

15. Generators: Functions that can pause and resume with yield.

16. WeakMap/WeakSet: Allow garbage collection of keys, useful for private data.

17. Proxy and Reflect: Intercept and customize fundamental object operations.

18. Symbol: Unique, immutable primitive for object property keys.

19. Optional Chaining (?.) and Nullish Coalescing (??): Safe property access and null/undefined defaults.

20. Module Systems: ES Modules (import/export) vs CommonJS (require/module.exports`).

Tips for JavaScript Interviews

1. Practice coding on platforms like JavaScript.ac 2. Explain your thought process out loud 3. Know the fundamentals — closures, prototypes, event loop 4. Understand async patterns — Promises, async/await 5. Be ready to write code on a whiteboard or shared editor