RMRM Full Stack & AI Engineer · All questions · Roadmaps
Languages · interview questions

JavaScript Interview Questions

JavaScript interview questions spanning core language fundamentals, ES6+ features, async programming, closures, prototypes, and runtime behavior — commonly asked at all levels from junior to senior roles.

1. What are the differences between var, let, and const?

beginner

var is function-scoped and hoisted (initialized as undefined); let and const are block-scoped and in the temporal dead zone until declared. const additionally prevents reassignment of the binding, though object properties can still be mutated.

2. What is hoisting in JavaScript?

beginner

Hoisting is JavaScript's behavior of moving declarations to the top of their scope during compilation. var declarations are hoisted and initialized as undefined; function declarations are fully hoisted; let and const are hoisted but not initialized, causing a ReferenceError if accessed before their declaration.

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

beginner

== performs type coercion before comparing (e.g., '5' == 5 is true), while === checks both value and type without coercion ('5' === 5 is false). Prefer === to avoid unexpected coercion bugs.

4. What are JavaScript data types?

beginner

JavaScript has 7 primitive types: string, number, bigint, boolean, undefined, null, and symbol. Everything else is an object (including arrays, functions, and dates). typeof null returns 'object' — a known historical quirk.

5. What is a closure?

intermediate

A closure is a function that retains access to its outer lexical scope even after the outer function has returned. This enables patterns like data encapsulation, factory functions, and memoization.

6. Explain the concept of 'this' in JavaScript.

intermediate

this refers to the execution context of a function. In a regular function it depends on how the function is called (the calling object); in arrow functions it is lexically inherited from the surrounding scope. Methods like call, apply, and bind can explicitly set this.

7. What is the event loop and how does it work?

intermediate

The event loop continuously checks whether the call stack is empty; if so, it dequeues tasks from the callback queue (macrotasks) or the microtask queue and pushes them onto the stack. Microtasks (Promise callbacks) are always drained before the next macrotask, giving them higher priority.

8. What is the difference between null and undefined?

beginner

undefined means a variable has been declared but not assigned a value, or a function parameter was not provided. null is an explicit assignment representing the intentional absence of a value. Both are falsy but null == undefined is true while null === undefined is false.

9. What are Promises and how do they differ from callbacks?

intermediate

A Promise is an object representing the eventual completion or failure of an asynchronous operation, providing .then(), .catch(), and .finally() chaining instead of nested callbacks. Promises avoid 'callback hell', offer better error propagation, and compose well with async/await syntax.

10. What is async/await and how does it work under the hood?

intermediate

async/await is syntactic sugar over Promises; an async function always returns a Promise, and await pauses execution of that function until the awaited Promise settles, then resumes with the resolved value. Under the hood the engine uses generator-like coroutines and the microtask queue.

11. What is prototypal inheritance?

intermediate

Every JavaScript object has an internal [[Prototype]] link to another object. When a property or method is not found on an object, the engine walks up the prototype chain until it finds it or reaches null. ES6 classes are syntactic sugar over this prototype-based system.

12. What is the difference between call, apply, and bind?

intermediate

All three set the this value of a function. call invokes the function immediately with arguments passed individually; apply invokes it immediately with arguments as an array; bind returns a new function with this permanently bound without invoking it immediately.

13. What are arrow functions and how do they differ from regular functions?

intermediate

Arrow functions have a concise syntax and do not have their own this, arguments, super, or new.target bindings — they inherit these lexically. They cannot be used as constructors and are not suitable for object methods that need a dynamic this.

14. What is event delegation?

intermediate

Event delegation attaches a single event listener to a parent element instead of multiple listeners on child elements, relying on event bubbling. When a child is clicked the event bubbles up to the parent, which checks event.target to determine the originating element — improving performance and handling dynamically added elements.

15. What is the difference between deep copy and shallow copy?

intermediate

A shallow copy duplicates only the top-level properties; nested objects still share the same reference. A deep copy recursively duplicates all nested structures. Shallow copies can be created with Object.assign or spread; deep copies with structuredClone(), JSON.parse(JSON.stringify()) (with caveats), or libraries like Lodash's cloneDeep.

16. Explain debouncing and throttling.

intermediate

Debouncing delays a function's execution until after a specified idle period following the last invocation — useful for search-input handlers. Throttling ensures a function executes at most once per specified interval regardless of how often it is called — useful for scroll or resize handlers.

17. What is the Temporal Dead Zone (TDZ)?

advanced

The TDZ is the period between entering a block scope and the point where a let or const variable is declared. Accessing the variable in this zone throws a ReferenceError, even though the declaration is technically hoisted.

18. How does JavaScript's garbage collection work?

advanced

Modern engines (V8, SpiderMonkey) primarily use mark-and-sweep: the GC marks all objects reachable from root references (globals, stack) and sweeps (frees) the rest. Generational collection separates short-lived (young/nursery) and long-lived (old/tenured) objects for efficiency.

19. What are WeakMap and WeakSet, and when would you use them?

advanced

WeakMap and WeakSet hold weak references to their object keys/values, meaning those objects can be garbage-collected even while held in the collection. They are useful for private data associated with DOM nodes or class instances without causing memory leaks, but they are not iterable and have no size property.

20. What is the difference between microtasks and macrotasks?

advanced

Macrotasks (setTimeout, setInterval, I/O callbacks) are queued in the task queue and processed one per event-loop iteration. Microtasks (Promise callbacks, queueMicrotask, MutationObserver) are queued in the microtask queue and all are drained completely after the current task and before the next macrotask, giving them priority over macrotasks.

Practice these out loud with an AI interviewer that grills you and grades your answers.
Open the app — free to start

© RM Full Stack & AI Engineer · All interview questions · Roadmaps · Open the app