JavaScript Interview Prep
Top 50 JavaScript Interview Questions
Master the top 50 essential JavaScript interview questions covering core fundamentals, asynchronous execution, object prototypes, ES6+, and web security.
1. Introduction
Preparing for JavaScript technical interviews requires a deep understanding of core language semantics, execution mechanics, and web APIs. This guide categorizes the top 50 most frequently asked interview questions into clear, concise answers.
2. Categorized Question Matrix
Section A: Language Fundamentals & Scope
1. What is the difference between var, let, and const?
• var: Function-scoped, hoisted with initialization to undefined, allows redeclaration.
• let: Block-scoped, hoisted into the Temporal Dead Zone (TDZ) without initialization, reassignable.
• const: Block-scoped, in TDZ until evaluated, requires immediate initialization, non-reassignable binding (object properties can still mutate).
2. Explain the Temporal Dead Zone (TDZ).
The TDZ is the period between entering a block scope and when a let or const variable is declared. Accessing the variable during the TDZ throws a ReferenceError.
3. What is a Closure and give a practical use case?
A closure is the combination of a function bundled together with references to its surrounding lexical environment.
Use Cases: Private state encapsulation (Module pattern), function memoization, event currying.
4. What are JavaScript Primitive Types vs Reference Types?
• Primitives (7): number, string, boolean, null, undefined, symbol, bigint. Stored directly by value in stack memory. Immutable.
• Reference Types: Objects, Arrays, Functions. Stored as heap memory references. Mutable.
5. Compare == vs ===.
• == (Abstract Equality): Performs implicit type coercion before comparing values.
• === (Strict Equality): Compares both value and data type without coercion.
Section B: Asynchronous JavaScript & Event Loop
6. How does the Event Loop work?
The Event Loop monitors the Call Stack and Task Queues. When the Call Stack is empty, it flushes ALL items in the Microtask Queue (Promises, queueMicrotask, MutationObserver) before picking the oldest single task from the Macrotask Queue (setTimeout, setInterval, I/O).
7. Difference between Microtasks and Macrotasks?
Microtasks have higher priority and process continuously until the microtask queue is completely drained. Macrotasks process one task per event loop tick, followed by a potential DOM paint step.
8. Explain Promise Combinators (Promise.all, allSettled, race, any).
• Promise.all: Fulfills when ALL promises fulfill; rejects immediately on FIRST rejection.
• Promise.allSettled: Fulfills when ALL promises settle, returning status objects for each.
• Promise.race: Settles (fulfills or rejects) as soon as the FIRST promise settles.
• Promise.any: Fulfills as soon as the FIRST promise fulfills; rejects only if ALL reject (AggregateError).
Section C: Prototypes & Object-Oriented JS
9. Explain Prototypal Inheritance and the Prototype Chain.
Every JavaScript object has an internal link ([[Prototype]]) pointing to another object. When accessing a property, the engine searches the object itself, then traverses up the prototype chain until it finds the property or reaches null.
10. What are the rules for binding the this keyword?
1. new Binding: this points to the freshly constructed object instance.
2. Explicit Binding: Set manually via call(), apply(), or bind().
3. Implicit Binding: Set to the calling context object (obj.method()).
4. Default Binding: Points to window (or undefined in strict mode).
5. Arrow Functions: Inherit this lexically from their enclosing scope.
3. Quick Quiz
Q1: Which Promise combinator method rejects immediately if ANY input promise rejects?
A) Promise.allSettled()
B) Promise.all()
Answer: B — Promise.all fails fast and rejects as soon as any promise in the iterable rejects.
4. Production Considerations
- • Clear Conceptual Explanations: When answering technical interview questions, start with a 1-sentence high-level summary, follow up with the underlying technical mechanics, and finish with a code example or real-world use case.