Cheatsheet
JavaScript Core — Interview Handbook
Condensed for last-minute review — every key takeaway, decision table, and recall card.
01 — FOUNDATION
Types, Values & Coercion
- Interview Tip: Memorize the coercion table for
==:nullandundefinedare equal to each other but not to anything else. For other types, JavaScript converts to numbers or strings. In interviews, always explain that===is safer and preferred.
- What is the difference between == and ===?
- — == performs type coercion before comparison; === does not (strict equality).
- Why is typeof null 'object'?
- — It's a historical bug in JavaScript's initial implementation that cannot be fixed for backward compatibility.
- How do you reliably check if a value is NaN?
- — Use Number.isNaN(value) to avoid coercion issues with the global isNaN().
- What is the purpose of Symbol?
- — To create unique, immutable identifiers, often used as object property keys to prevent name collisions.
02 — FOUNDATION
Scope, Closures & Hoisting
- Interview Tip: When asked about hoisting, always mention the temporal dead zone for
letandconst. Interviewers love to see that you understand the difference between hoisting (declaration moved) and initialization (value assignment). A common trick question: 'What doesconsole.log(a)output beforelet a = 5?' The answer is a ReferenceError, not undefined.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function scope | Block scope | Block scope |
| Hoisting | Hoisted (initialized as undefined) | Hoisted (TDZ) | Hoisted (TDZ) |
| Reassignment | Allowed | Allowed | Not allowed |
| Redeclaration | Allowed | Not allowed | Not allowed |
- What is the temporal dead zone (TDZ)?
- — The period between entering a scope and the declaration of a
letorconstvariable, during which accessing the variable throws a ReferenceError. - How does hoisting differ between var and let/const?
- —
varis hoisted and initialized withundefined.letandconstare hoisted but not initialized (TDZ). - What is a closure?
- — A function that retains access to its lexical scope even when executed outside that scope.
- What is an IIFE and why is it used?
- — An Immediately Invoked Function Expression that creates a new scope to avoid global variable pollution and provide data privacy.
03 — FOUNDATION
Functions, this & Execution
- Interview Tip: this in Event Handlers: In DOM event handlers,
thisrefers to the element that fired the event when using a regular function. With an arrow function,thiscomes from the surrounding lexical context (e.g., the class instance). Interviewers often ask about this difference—be ready to explain it with an example.
- What is the difference between function declaration and function expression regarding hoisting?
- — Function declarations are hoisted (can be called before definition); function expressions are not hoisted (must be defined before use).
- How does `this` behave differently in arrow functions vs regular functions?
- — Arrow functions inherit `this` from the enclosing lexical scope; regular functions have their own `this` determined by how they are called.
- What is the purpose of `bind`?
- — `bind` creates a new function with a permanently bound `this` value (and optional partial arguments) that can be called later.
- What is a key advantage of rest parameters over the `arguments` object?
- — Rest parameters provide a real array, allowing use of array methods like `map`, `filter`, and `reduce` directly.
- What is currying in JavaScript?
- — Currying transforms a function with multiple arguments into a sequence of nested functions, each taking a single argument, enabling partial application.
04 — CORE
Prototypes & Inheritance
- Interview Tip: Distinguish Own vs Inherited Properties: Interviewers often ask how to tell if a property is on the object itself or inherited. Use
hasOwnProperty(orObject.hasOwnin modern JS) to check. For example:obj.hasOwnProperty('toString')returnsfalsebecausetoStringis inherited fromObject.prototype.
- What is the prototype chain?
- — A series of links between objects, where each object has a reference to its prototype. Property lookup traverses this chain until found or null.
- How does Object.create work?
- — Object.create(proto) creates a new object with proto as its prototype. Optionally accepts a properties descriptor object.
- What does instanceof check?
- — It checks if the prototype property of a constructor appears anywhere in an object's prototype chain.
- How do mixins work in JavaScript?
- — Mixins are objects with methods that are copied into another object's prototype using Object.assign, allowing composition of behavior.
05 — CORE
Event Loop & Asynchrony
- Interview Tip: Microtask Starvation: If you recursively enqueue microtasks (e.g., a promise chain that never resolves), the microtask queue never empties, blocking macrotasks like rendering or
setTimeout. This is a common performance pitfall—always ensure microtasks eventually yield.
| Feature | Browser | Node.js |
|---|---|---|
| Microtask execution | After each macrotask | After each phase |
| requestAnimationFrame | Yes, before repaint | Not available |
| setImmediate | Not available | Yes, in check phase |
| I/O handling | Event-driven (e.g., fetch) | libuv thread pool |
- What is the event loop?
- — A mechanism that continuously checks the call stack and queues, pushing callbacks from task/microtask queues onto the stack when it's empty.
- Difference between macrotask and microtask?
- — Macrotasks (setTimeout, I/O) are processed one per loop iteration; microtasks (promises, queueMicrotask) are all processed after each macrotask.
- When does requestAnimationFrame run?
- — After microtasks but before the next browser repaint, synchronized with the display refresh rate.
- What is setImmediate in Node.js?
- — A macrotask that runs in the check phase, after I/O callbacks but before timers (if scheduled in the same phase).
06 — CORE
Promises & Async/Await
- Interview Tip: Error Handling in Promise.all: Remember that
Promise.allfails fast—if any promise rejects, the entire promise rejects immediately. UsePromise.allSettledwhen you need results from all promises regardless of failures, such as batch API calls where partial success is acceptable.
- What are the three states of a Promise?
- — Pending, Fulfilled, Rejected
- How does Promise.allSettled differ from Promise.all?
- — Promise.allSettled never rejects; it waits for all promises to settle and returns an array of result objects with status and value/reason.
- What does an async function always return?
- — A Promise. If the function returns a value, it's wrapped in a resolved promise; if it throws, it's a rejected promise.
- Why is using forEach with async/await problematic?
- — forEach does not await promises; it fires all callbacks concurrently. Use for...of or Promise.all for sequential or parallel async operations.
07 — MODERN JS
ES6+ & Modern Syntax
- Interview Tip: When asked about optional chaining, emphasize that it short-circuits—if a property is null/undefined, the entire chain returns undefined without evaluating further. This prevents runtime errors in deeply nested data.
- What is the difference between spread and rest operators?
- — Spread expands an iterable into elements (used in arrays/objects). Rest collects multiple elements into an array (used in function parameters).
- How does optional chaining prevent errors?
- — It short-circuits and returns undefined if a property is null/undefined, instead of throwing a TypeError.
- What is a generator function?
- — A function declared with function* that can pause execution using yield and resume later, returning an iterator.
- Why use WeakMap instead of Map?
- — WeakMap holds weak references to keys (objects), allowing garbage collection when no other references exist, preventing memory leaks.
08 — MODERN JS
Modules & the Build Pipeline
- Interview Tip: When asked about tree shaking, mention that bundlers mark modules with
sideEffects: falseinpackage.jsonto safely remove unused code. Also note that dynamic imports can break tree shaking because the module is loaded at runtime.
- What is the key difference between ES modules and CommonJS?
- — ES modules use static import/export syntax (analyzed at parse time), while CommonJS uses dynamic require() (evaluated at runtime). ESM supports tree shaking; CJS does not.
- What is tree shaking?
- — A build-time optimization that removes unused exports from the final bundle, relying on static analysis of ES module imports.
- What is a circular dependency and why is it problematic?
- — When two modules import each other. In CJS, it can cause undefined values because module.exports is not fully populated. ESM handles it better with live bindings, but cycles should still be avoided.
- What is top-level await?
- — A feature in ES modules that allows using await outside of async functions, blocking module execution until the promise resolves. Useful for async module initialization.
09 — ADVANCED
Patterns & Functional Techniques
- Interview Tip: When implementing debounce or throttle, always handle the
thiscontext correctly usingfn.apply(this, args). Interviewers often check for this subtlety.
- What is the module pattern?
- — Encapsulates private state using closures, exposing only a public API to avoid global scope pollution.
- Difference between debounce and throttle?
- — Debounce delays execution until after a pause; throttle limits execution to once per interval.
- What does memoization cache?
- — Function return values based on arguments, avoiding recomputation for pure functions.
- Factory vs Constructor: key difference?
- — Constructors use 'new' and prototype inheritance; factories return any object without 'new', offering more flexibility.
- How to update an object immutably?
- — Use spread operator or Object.assign to create a new object with the desired changes, leaving the original unchanged.
10 — ADVANCED
Memory Management & Performance
- Interview Tip: When asked about closure leaks, mention that modern engines optimize by not retaining unused variables, but
evalorwithcan break this. Always nullify references when removing listeners.
- What algorithm does JavaScript use for garbage collection?
- — Mark-and-sweep: marks reachable objects from roots, then sweeps unmarked ones.
- How does WeakMap help with memory management?
- — It holds weak references to keys, so if no other reference exists, the key-value pair is garbage collected.
- What is a hidden class in V8?
- — An internal structure that tracks object property layout for optimized access; consistent property order improves performance.
- Name two common memory leak sources.
- — Global variables and forgotten timers/event listeners that retain references.