DEV Community

Cover image for Core JavaScript Expert Revision Handbook
Animesh Pandey
Animesh Pandey

Posted on • Edited on

Core JavaScript Expert Revision Handbook

๐Ÿ“‘ Table of Contents


๐ŸŸฆ Part 1

This section covers Closures, Scope & Hoisting, this, Prototypes, and the Event Loop โ€” the foundation of almost every frontend interview.


1. ๐Ÿ”’ Closures

Definition:
A closure is a function that retains access to its lexical environment (scope chain) even after the outer function has finished executing.

โœ… Key Points

  • JavaScript uses lexical scoping, not dynamic scoping.
  • Closures are formed naturally whenever a function is defined inside another.
  • Enable data privacy, currying, memoization, event handlers.

โš ๏ธ Gotchas

  • Loop with var:
  for (var i = 0; i < 3; i++) {
    setTimeout(() => console.log(i), 100);
  }
  // 3, 3, 3 (same var captured)
Enter fullscreen mode Exit fullscreen mode

Fix with let (block-scoped) or IIFE.

  • Memory leaks:
    Closures capturing large objects (like DOM nodes) can prevent garbage collection.

  • Debugging pain:
    DevTools may show โ€œunexpectedโ€ retained variables due to closures.

๐Ÿ’ก Real-world Bug

function registerHandlers(nodes) {
  for (var i = 0; i < nodes.length; i++) {
    nodes[i].onclick = () => console.log("Clicked:", i);
  }
}
// All handlers print last index!
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œA closure is a function with its lexical scope bundled in. It enables encapsulation and callbacks but can cause bugs with loops (var) and memory leaks if you capture more than needed.โ€


2. ๐Ÿ“ฆ Scope & Hoisting

Definition:
Scope defines where variables are accessible. Hoisting means declarations are moved to the top of their scope during compilation.

โœ… Key Points

  • var โ†’ function-scoped, hoisted & initialized as undefined.
  • let/const โ†’ block-scoped, hoisted but uninitialized (Temporal Dead Zone, TDZ).
  • Function declarations โ†’ fully hoisted with body.
  • Function expressions โ†’ behave like variables.

โš ๏ธ Gotchas

  • TDZ Example:
  console.log(x); // ReferenceError
  let x = 10;
Enter fullscreen mode Exit fullscreen mode
  • Function in block scope:
  if (true) {
    function foo() {}
  }
  foo(); // Works differently in strict vs sloppy mode
Enter fullscreen mode Exit fullscreen mode
  • Shadowing:
  let x = 1;
  {
    let x = 2;
    console.log(x); // 2 (outer x hidden)
  }
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Quick Table

Keyword Scope Hoisting Default Init
var Function Yes undefined
let Block Yes TDZ (error)
const Block Yes TDZ (error)
Function Function Yes (with body) -

๐ŸŽฏ Interview One-Liner

โ€œDeclarations are hoisted. var hoists with undefined; let/const hoist into the TDZ; functions hoist with their body. Thatโ€™s why accessing a let before declaration throws, while var gives undefined.โ€


3. ๐Ÿงญ The this Keyword

Definition:
this is determined by the call site, not the definition.

โœ… Rules of this Binding (Precedence)

  1. new binding โ†’ new Foo()
  2. explicit binding โ†’ call, apply, bind
  3. implicit binding โ†’ obj.method()
  4. default binding โ†’ global (window) or undefined (strict mode)

โš ๏ธ Gotchas

  • Losing context:
  const obj = { x: 10, f() { console.log(this.x); } };
  setTimeout(obj.f, 0); // undefined (lost `this`)
Enter fullscreen mode Exit fullscreen mode
  • Arrow functions: capture lexical this, cannot be rebound.
  • bind + call combo:
  function f() { console.log(this.x); }
  const bound = f.bind({ x: 1 });
  bound.call({ x: 2 }); // still 1 (bind wins)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Quick Table

Call Type this Value
Global fn (non-strict) window
Global fn (strict) undefined
Method call obj.fn() obj
new Fn() New instance
Bound fn Bound object
Arrow fn Lexical scope

๐ŸŽฏ Interview One-Liner

โ€œthis is set at call time, not definition. Precedence is: new > explicit (bind) > implicit (object call) > default (global/undefined). Arrow functions inherit this lexically.โ€


4. ๐Ÿงฉ Prototypes & Inheritance

Definition:
Objects inherit properties via the prototype chain ([[Prototype]]).

โœ… Key Points

  • Object.create(proto) โ†’ creates object with proto as prototype.
  • Functions have a .prototype used when called with new.
  • Property lookup climbs prototype chain.

โš ๏ธ Gotchas

  • Modifying .prototype after instances exist โ†’ they wonโ€™t see changes.
  • Shadowing: own property hides prototype property.
  • Deep prototype chains slow lookups.

Code Example

function Animal(name) { this.name = name; }
Animal.prototype.speak = function() { console.log(this.name + " makes noise"); }

const dog = new Animal("Rex");
dog.speak(); // "Rex makes noise"

console.log(dog.__proto__ === Animal.prototype); // true
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œObjects inherit via prototypes. If a property isnโ€™t found, JS looks up the chain. Constructors set their .prototype as the prototype of new instances.โ€


5. โณ Event Loop & Concurrency

Definition:
The event loop coordinates execution: it runs the call stack, then processes the task queues (macrotasks and microtasks).

โœ… Key Points

  • Macrotasks: setTimeout, setInterval, I/O.
  • Microtasks: Promises, MutationObserver, queueMicrotask.
  • After each macrotask, the loop drains all microtasks.

โš ๏ธ Gotchas

  • setTimeout(fn, 0) is not immediate โ€” runs after microtasks.
  • Promise.then always executes before timeouts.
  • In Node.js, process.nextTick runs before microtasks.
  • Nested promises keep chaining microtasks โ†’ possible starvation.

Code Example

console.log("A");

setTimeout(() => console.log("B"), 0);

Promise.resolve().then(() => console.log("C"));

console.log("D");

// Output: A, D, C, B
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Execution Order

  1. Run call stack
  2. Drain microtask queue
  3. Run next macrotask
  4. Repeat

๐ŸŽฏ Interview One-Liner

โ€œThe event loop runs stack โ†’ microtasks โ†’ macrotasks. Thatโ€™s why Promise.then callbacks run before setTimeout(fn, 0).โ€


๐ŸŸฆ Asynchronous JavaScript


1. โฑ๏ธ Timers (setTimeout, setInterval)

Definition:
setTimeout(fn, delay) schedules a function once after delay; setInterval(fn, delay) repeats at intervals.

โœ… Key Points

  • Actual delay is minimum โ€” not guaranteed exact.
  • Nested timers can be throttled by browsers (clamp to โ‰ฅ4ms after many calls, 1000ms when tab inactive).
  • clearTimeout / clearInterval cancel scheduled tasks.

โš ๏ธ Gotchas

  • setTimeout(fn, 0) runs after all microtasks (not immediate).
  • Long-running tasks block timers (since JS is single-threaded).
  • setInterval can drift โ†’ better to use recursive setTimeout for accuracy.

Example

setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
// Output: promise, timeout
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œTimers schedule macrotasks. setTimeout(fn, 0) isnโ€™t immediate โ€” it runs after microtasks. For precise intervals, recursive setTimeout is safer than setInterval.โ€


2. ๐Ÿ”— Promises

Definition:
A Promise is an object representing the eventual result of an async operation.

โœ… Key Points

  • States: pending โ†’ fulfilled (resolved) / rejected.
  • .then and .catch return new promises โ†’ enable chaining.
  • Handlers are always async (enqueued as microtasks).

โš ๏ธ Gotchas

  • Unhandled rejection:
  Promise.reject("err");
  // Without .catch โ†’ unhandled rejection warning
Enter fullscreen mode Exit fullscreen mode
  • .then handlers always async:
  Promise.resolve().then(() => console.log("then"));
  console.log("sync");
  // sync, then
Enter fullscreen mode Exit fullscreen mode
  • Multiple .then on the same promise all execute independently.
  • Returning a promise inside .then flattens it (automatic chaining).

Example

Promise.resolve(1)
  .then(x => x + 1)
  .then(x => Promise.resolve(x + 1))
  .then(console.log); // 3
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œPromises represent async results. .then handlers are always microtasks, so they run before timers. Returning a promise in .then flattens the chain.โ€


3. โšก async / await

Definition:
Syntactic sugar over promises. async functions return promises; await pauses until promise settles.

โœ… Key Points

  • await only works inside async functions (or top-level in modules).
  • Execution is split โ†’ code after await runs in a microtask.
  • Parallelize with Promise.all to avoid serial awaits.

โš ๏ธ Gotchas

  • await inside loops โ†’ serial execution (slow).
  for (const u of users) {
    await fetch(u); // slow
  }
  await Promise.all(users.map(u => fetch(u))); // fast
Enter fullscreen mode Exit fullscreen mode
  • try/catch is needed โ€” await throws on rejection.
  • Mixing await with non-promises just wraps in Promise.resolve.

Example

async function f() {
  console.log("A");
  await Promise.resolve();
  console.log("B");
}
f();
console.log("C");

// Output: A, C, B
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œasync/await is syntax sugar over promises. await pauses execution and resumes in a microtask. Using it inside loops serializes calls โ€” use Promise.all for parallelism.โ€


4. ๐ŸŽ›๏ธ Generators & Async Generators

Definition:
Generators (function*) are functions that can pause and resume using yield. Async generators use for await...of.

โœ… Key Points

  • Generators return an iterator with .next().
  • Useful for building custom async workflows (before promises).
  • co library + generators โ†’ pre-async/await async management.

โš ๏ธ Gotchas

  • Generators donโ€™t manage async by themselves; must yield promises and have a runner.
  • Async generators (async function*) combine promises with iteration.

Example

function* gen() {
  yield 1;
  yield 2;
}
const g = gen();
console.log(g.next()); // { value: 1, done: false }
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œGenerators pause with yield and resume later. Theyโ€™re useful for async flow control, but in modern JS, async/await replaces most generator use cases.โ€


5. โณ Debounce & Throttle

Definition:
Patterns for rate-limiting function execution.

  • Debounce: wait until no calls happen for X ms. (Good for search input).
  • Throttle: allow one call per X ms. (Good for scroll/resize).

Example

function debounce(fn, delay) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

function throttle(fn, delay) {
  let last = 0;
  return (...args) => {
    const now = Date.now();
    if (now - last >= delay) {
      fn(...args);
      last = now;
    }
  };
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œDebounce delays execution until events stop; throttle limits execution to once per interval. Debounce is for inputs, throttle for continuous events like scroll.โ€


6. ๐ŸŽจ requestAnimationFrame (rAF)

Definition:
requestAnimationFrame(callback) schedules a callback before the next repaint (~16.6ms at 60fps).

โœ… Key Points

  • More efficient than setTimeout for animations.
  • Pauses when tab is inactive (better for performance).
  • Ideal for smooth visual updates.

โš ๏ธ Gotchas

  • Doesnโ€™t guarantee 60fps โ€” depends on device refresh rate.
  • Can be canceled with cancelAnimationFrame(id).

Example

function animate() {
  // draw frame
  requestAnimationFrame(animate);
}
animate();
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œrequestAnimationFrame runs a callback before the next repaint. Itโ€™s optimized for animations and pauses in inactive tabs.โ€


7. ๐Ÿงต Web Workers

Definition:
Web Workers run JS in background threads, off the main event loop.

โœ… Key Points

  • Communicate via postMessage (structured clone).
  • No DOM access inside workers.
  • Good for CPU-heavy tasks.

โš ๏ธ Gotchas

  • Serialization overhead in postMessage.
  • SharedArrayBuffer needed for true shared memory.
  • Not supported in all contexts (e.g., some cross-origin iframes).

Example

// worker.js
self.onmessage = e => {
  self.postMessage(e.data * 2);
};

// main.js
const worker = new Worker("worker.js");
worker.onmessage = e => console.log("Result:", e.data);
worker.postMessage(10); // Result: 20
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œWeb Workers move heavy computation off the main thread. They communicate via messages, but canโ€™t access the DOM.โ€


๐ŸŸฆ Advanced Objects & Memory


1. ๐Ÿงน Garbage Collection (GC)

Definition:
JavaScript uses automatic garbage collection โ€” freeing memory for objects that are no longer reachable.

โœ… Key Points

  • Reachability = if an object can be accessed via a reference chain from a root (e.g. window or stack).
  • Common GC strategy: Mark & Sweep.
  • Circular references are fine if unreachable from root.

โš ๏ธ Gotchas

  • Accidental leaks:

    • Global variables (window.leak = obj).
    • Closures holding large structures.
    • Detached DOM nodes retained in memory.
  • Timers / event listeners: If not cleared, they keep references alive.

Example

let el = document.getElementById("btn");
el.onclick = () => console.log("clicked");
// If `el` removed from DOM but reference stays in closure โ†’ leak
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œGC frees unreachable memory. Leaks usually happen via globals, closures, or dangling DOM references, not because of circular references.โ€


2. ๐Ÿ—๏ธ Symbols

Definition:
Symbol() creates a unique primitive identifier.

โœ… Key Points

  • Never equal to another symbol, even with same description.
  • Used for non-colliding property keys.
  • Symbol.for(key) reuses a global registry symbol.
  • Built-in symbols customize behavior (Symbol.iterator, Symbol.toStringTag).

โš ๏ธ Gotchas

  • for...in and Object.keys ignore symbols. Must use Object.getOwnPropertySymbols.
  • JSON.stringify ignores symbols.

Example

const id = Symbol("id");
const obj = { [id]: 123 };
console.log(Object.keys(obj)); // []
console.log(Object.getOwnPropertySymbols(obj)); // [Symbol(id)]
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œSymbols are unique identifiers, often used as hidden object keys or to customize built-in behaviors. Theyโ€™re ignored by JSON and normal enumeration.โ€


3. ๐Ÿ—„๏ธ WeakMap & WeakSet

Definition:
Weak collections hold weak references to objects โ€” allowing GC when there are no other references.

โœ… Key Points

  • WeakMap: keys must be objects, values arbitrary.
  • WeakSet: stores objects, prevents duplicates.
  • Entries are not enumerable (no size, no iteration).

โš ๏ธ Gotchas

  • Canโ€™t inspect contents โ†’ intentionally opaque.
  • Useful for private data or DOM element caching.

Example

let wm = new WeakMap();
let obj = {};
wm.set(obj, "secret");

console.log(wm.get(obj)); // "secret"
obj = null; // entry auto-removed when GC runs
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œWeakMap/WeakSet hold weak references โ€” objects can be GCโ€™d even if theyโ€™re keys. Theyโ€™re ideal for caching and private data without leaks.โ€


4. ๐Ÿ“ Property Descriptors

Definition:
Every property has a descriptor object with attributes:

  • value
  • writable
  • enumerable
  • configurable (or get/set for accessors).

โœ… Key Points

  • Object.defineProperty controls property behavior.
  • enumerable controls visibility in loops.
  • configurable prevents deletion or redefinition.
  • Non-writable prevents assignment.

โš ๏ธ Gotchas

  • Default attributes: false if defined via defineProperty.
  • Freezing (Object.freeze) sets all writable=false, configurable=false.

Example

const obj = {};
Object.defineProperty(obj, "x", {
  value: 42,
  writable: false,
  enumerable: true,
  configurable: false
});
obj.x = 100; // ignored in strict mode โ†’ TypeError
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œProperty descriptors let you control attributes like writable, enumerable, configurable. Theyโ€™re the foundation of Object.freeze and getters/setters.โ€


5. ๐Ÿ•ต๏ธ Proxy & Reflect

Definition:
Proxy allows you to intercept operations on objects. Reflect provides low-level methods that mirror default behavior.

โœ… Key Points

  • Handlers (traps): get, set, has, deleteProperty, apply, construct.
  • Reflect ensures correct forwarding (avoids reinventing default behavior).
  • Used for logging, validation, virtualization, reactive systems (Vue3 uses Proxy).

โš ๏ธ Gotchas

  • Proxies can slow performance if overused.
  • Can break identity expectations (obj === proxy is false).
  • Some operations (like private fields) are not trap-able.

Example

const obj = { a: 1 };
const proxy = new Proxy(obj, {
  get(target, prop) {
    console.log("get", prop);
    return target[prop];
  }
});

console.log(proxy.a); // logs: get a โ†’ 1
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œProxies wrap objects with traps for operations like get/set. Reflect provides the default behavior to forward calls. Proxies power modern reactivity systems.โ€


6. ๐ŸงŠ Immutability Tricks

Definition:
JavaScript objects are mutable by default. Immutability ensures predictable state (important in React/Redux).

โœ… Key Points

  • Object.freeze โ†’ prevents modifications.
  • Object.seal โ†’ prevents adding/removing props, but can update values.
  • Object.preventExtensions โ†’ prevents new props, but allows edits/deletes.
  • Deep immutability requires recursion or libraries (immer).

โš ๏ธ Gotchas

  • Freeze is shallow โ€” nested objects still mutable.
  • Frozen objects are still extensible by changing prototype unless explicitly blocked.

Example

const obj = Object.freeze({ x: 1, y: { z: 2 } });
obj.x = 10; // ignored
obj.y.z = 20; // allowed (nested not frozen)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œObjects are mutable by default. freeze/seal/preventExtensions restrict changes, but only shallowly. For deep immutability, you need recursion or libraries like Immer.โ€


๐ŸŸฆ Advanced Functions & ES6+ Features


1. ๐Ÿ—๏ธ Higher-Order Functions (HOFs)

Definition:
Functions that either take functions as arguments or return functions.

โœ… Key Points

  • Foundation of functional programming.
  • Enable: map, filter, reduce, decorators, middleware.
  • Encourage immutability + composition.

โš ๏ธ Gotchas

  • Passing non-functions โ†’ runtime errors.
  • Excessive nesting โ†’ callback hell (before Promises/async).

Example

function withLogging(fn) {
  return (...args) => {
    console.log("Calling with", args);
    return fn(...args);
  };
}
const sum = (a, b) => a + b;
const loggedSum = withLogging(sum);
loggedSum(2, 3); // logs args โ†’ 5
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œA higher-order function either takes functions as arguments or returns them. They enable abstractions like map, filter, and middleware.โ€


2. ๐ŸŽจ Currying & Partial Application

Definition:

  • Currying: Transforming a function f(a, b, c) into f(a)(b)(c).
  • Partial application: Pre-filling some arguments, returning a new function.

โœ… Key Points

  • Used in FP libraries like Lodash, Ramda.
  • Helps reusability and function composition.

โš ๏ธ Gotchas

  • Over-currying โ†’ unreadable code.
  • Currying โ‰  partial application (though related).

Example

const currySum = a => b => c => a + b + c;
currySum(1)(2)(3); // 6

function partial(fn, ...fixed) {
  return (...rest) => fn(...fixed, ...rest);
}
const add = (a, b) => a + b;
const add5 = partial(add, 5);
add5(10); // 15
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œCurrying breaks a function into unary steps (f(a)(b)(c)), while partial application pre-fills arguments. Both improve reusability.โ€


3. ๐Ÿ“ฆ Modules (ESM vs CommonJS)

Definition:
Modules encapsulate code into reusable files.

โœ… Key Points

  • CommonJS (CJS): Node.js legacy (require, module.exports).
  • ES Modules (ESM): Modern JS (import, export).
  • ESM is statically analyzable โ†’ enables tree-shaking.
  • Default exports vs named exports.

โš ๏ธ Gotchas

  • Mixing CJS and ESM leads to quirks (default interop).
  • ESM is always strict mode.
  • Imports are hoisted (run before any other code).

Example

// utils.js
export function add(a, b) { return a + b; }
export default function subtract(a, b) { return a - b; }

// main.js
import subtract, { add } from "./utils.js";
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œCommonJS uses require, ES Modules use import/export. ESM is statically analyzable and supports tree-shaking, which is why modern bundlers prefer it.โ€


4. โœจ Destructuring & Spread/Rest

Definition:
Syntax for unpacking values into variables.

โœ… Key Points

  • Array destructuring: [a, b] = arr.
  • Object destructuring: { x, y } = obj.
  • Rest: gather leftovers.
  • Spread: expand arrays/objects.

โš ๏ธ Gotchas

  • Nested destructuring โ†’ undefined if property missing.
  • Default values only apply when property is undefined, not null.

Example

const [a, b = 5] = [1];
console.log(a, b); // 1, 5

const { x, y: z } = { x: 10, y: 20 };
console.log(z); // 20

const arr = [1, 2];
const newArr = [...arr, 3]; // [1,2,3]
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œDestructuring unpacks arrays/objects, rest gathers leftovers, spread expands. Defaults only trigger on undefined, not null.โ€


5. ๐Ÿงพ Default, Rest, and Named Parameters

โœ… Key Points

  • Default params:
  function f(x = 10) { return x; }
  f(); // 10
Enter fullscreen mode Exit fullscreen mode
  • Rest params: collects extra args โ†’ array.
  • Named params: simulated with object destructuring.

โš ๏ธ Gotchas

  • Default params are evaluated at call time.
  • Rest params differ from arguments (rest is real array, arguments is array-like).

๐ŸŽฏ Interview One-Liner

โ€œDefault params evaluate at call time, rest params gather extras as a real array, and named params are simulated via object destructuring.โ€


6. ๐Ÿ”„ Iterators & Iterables

Definition:
An iterator is an object with .next(). An iterable has [Symbol.iterator].

โœ… Key Points

  • Iterables: arrays, strings, maps, sets.
  • for...of loops over iterables (not plain objects).
  • Custom iterables by implementing [Symbol.iterator].

โš ๏ธ Gotchas

  • Plain objects arenโ€™t iterable unless you define [Symbol.iterator].
  • Spread ...obj works only if iterable defined.

Example

const iterable = {
  *[Symbol.iterator]() {
    yield 1; yield 2; yield 3;
  }
};
for (const x of iterable) console.log(x);
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œIterables implement [Symbol.iterator], which returns an iterator with .next(). Thatโ€™s why arrays, maps, and sets work with for...of and spread.โ€


7. ๐Ÿ“š Map, Set, WeakMap, WeakSet

Definition:
Specialized collection types introduced in ES6.

โœ… Key Points

  • Map: key-value pairs, keys can be any type.
  • Set: unique values.
  • WeakMap/WeakSet: only object keys, weak references (non-enumerable).

โš ๏ธ Gotchas

  • NaN is considered equal to itself in Set.
  • Map preserves insertion order.
  • WeakMap doesnโ€™t prevent GC.

Example

const set = new Set([1, 2, 2, 3]);
console.log(set.size); // 3

const map = new Map();
map.set("a", 1).set({}, 2);
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œMap and Set are collections with insertion order preserved. WeakMap/WeakSet hold weak object references, useful for caching without memory leaks.โ€


8. ๐Ÿงญ ES6+ Extras (that interviewers test)

๐Ÿ”‘ Template Literals

const name = "Alex";
console.log(`Hello ${name}`);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Tagged Templates

function tag(strings, ...values) {
  return strings[0] + values.map(v => v.toUpperCase()).join("");
}
console.log(tag`Hi ${"alex"} and ${"bob"}`); // "Hi ALEXandBOB"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Optional Chaining & Nullish Coalescing

obj?.prop?.nested; // avoids TypeError
val ?? "default";  // only default if null/undefined
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œModern JS adds quality-of-life features like template literals, optional chaining (?.), and nullish coalescing (??). These avoid common boilerplate and bugs.โ€


๐ŸŸฆ Browser & DOM Fundamentals


1. ๐ŸŽฏ Event Delegation

Definition:
Attaching a single event listener on a parent to handle events for multiple child elements via event bubbling.

โœ… Key Points

  • Uses event propagation (capture โ†’ target โ†’ bubble).
  • Reduces memory usage (no per-child listeners).
  • Works well for dynamic DOM (children added later).

โš ๏ธ Gotchas

  • event.target vs event.currentTarget:

    • target = actual clicked element.
    • currentTarget = element with listener.
  • Some events donโ€™t bubble (e.g., blur, focus).

Example

document.getElementById("list").addEventListener("click", e => {
  if (e.target.tagName === "LI") {
    console.log("Clicked item:", e.target.textContent);
  }
});
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œEvent delegation attaches a single listener on a parent and relies on bubbling. Itโ€™s efficient for many/dynamic child elements.โ€


2. ๐Ÿ–ผ๏ธ Reflow vs Repaint (Performance)

Definition:

  • Reflow (layout): Browser recalculates element positions & sizes.
  • Repaint: Browser redraws pixels (e.g., color change).

โœ… Key Points

  • Reflow is more expensive than repaint.
  • Causes of reflow: DOM changes, style changes, window resize, font load.
  • Batch DOM reads/writes to avoid multiple reflows.

โš ๏ธ Gotchas

  • Accessing layout properties (offsetHeight, scrollTop) forces reflow.
  • Animating top/left triggers layout; animating transform/opacity is GPU-optimized.

Example

const el = document.getElementById("box");
el.style.width = "200px";          // triggers reflow
console.log(el.offsetHeight);      // forces reflow again (expensive)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œReflow recalculates layout, repaint redraws pixels. Reflow is costly โ€” optimize by batching DOM changes and animating transform/opacity.โ€


3. ๐ŸŒณ DOM Traversal & Manipulation

โœ… Methods

  • getElementById, querySelector, querySelectorAll.
  • parentNode, children, nextSibling.
  • Creating: document.createElement, appendChild, insertBefore.
  • Performance: Use DocumentFragment for batch insertions.

โš ๏ธ Gotchas

  • innerHTML is faster for large inserts but unsafe (XSS risk).
  • NodeList vs HTMLCollection:

    • NodeList can be static or live.
    • HTMLCollection is always live.

๐ŸŽฏ Interview One-Liner

โ€œDOM traversal uses APIs like querySelector, parentNode, and siblings. For performance, batch inserts with DocumentFragment instead of repeated appendChild.โ€


4. ๐Ÿ’พ Storage APIs

โœ… Key Points

  • Cookies:

    • 4KB limit.
    • Sent with every HTTP request.
    • Expiration & domain/path scoped.
  • localStorage:

    • 5โ€“10MB.
    • Synchronous API.
    • Persistent until cleared.
  • sessionStorage:

    • Per-tab/session.
  • IndexedDB:

    • Async, NoSQL DB in browser.
    • Large storage, structured queries.

โš ๏ธ Gotchas

  • localStorage is blocking โ†’ avoid heavy writes in main thread.
  • Cookies hurt performance since theyโ€™re sent on every request.
  • IndexedDB APIs are clunky (usually use wrapper libraries).

๐ŸŽฏ Interview One-Liner

โ€œCookies are for server comms (small, auto-sent), localStorage/sessionStorage for small client-only data, and IndexedDB for large structured storage.โ€


5. ๐Ÿ”’ Browser Security (CORS, CSRF, XSS)

โœ… CORS (Cross-Origin Resource Sharing)

  • Controls cross-domain requests.
  • Server sets Access-Control-Allow-Origin.

โœ… CSRF (Cross-Site Request Forgery)

  • Malicious site tricks userโ€™s browser into sending authenticated request.
  • Prevented with CSRF tokens or SameSite cookies.

โœ… XSS (Cross-Site Scripting)

  • Injected scripts executed in victimโ€™s browser.
  • Prevented with escaping, CSP (Content Security Policy).

๐ŸŽฏ Interview One-Liner

โ€œCORS controls which origins can access resources, CSRF tricks users into sending unwanted requests, and XSS injects malicious scripts. Fix with headers, tokens, and sanitization.โ€


6. โšก Service Workers

Definition:
Background scripts that intercept network requests โ†’ enable offline caching, push notifications, background sync.

โœ… Key Points

  • Use Cache API for offline storage.
  • Run in their own thread (no DOM access).
  • Lifecycle: install โ†’ activate โ†’ fetch.

โš ๏ธ Gotchas

  • Require HTTPS.
  • Debugging can be tricky (stale workers โ†’ need manual refresh).
  • Misuse can cause cache-bloat or stale responses.

Example

self.addEventListener("fetch", e => {
  e.respondWith(
    caches.match(e.request).then(res => res || fetch(e.request))
  );
});
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œService workers are background scripts that enable offline caching and network interception. They power PWAs but must be carefully managed to avoid stale caches.โ€


7. ๐ŸŒ Critical Rendering Path (CRP)

Definition:
Steps the browser takes to convert HTML/CSS/JS into pixels.

โœ… Stages

  1. Parse HTML โ†’ DOM
  2. Parse CSS โ†’ CSSOM
  3. Combine โ†’ Render Tree
  4. Layout (Reflow)
  5. Paint (Repaint)
  6. Composite layers

โš ๏ธ Gotchas

  • Render-blocking: CSS blocks rendering, JS blocks parsing unless defer/async.
  • Fonts: FOUT/FOIT issues (Flash of Unstyled/Invisible Text).

๐ŸŽฏ Interview One-Liner

โ€œThe CRP is DOM + CSSOM โ†’ render tree โ†’ layout โ†’ paint โ†’ composite. Optimize by deferring scripts, minimizing CSS, and preloading fonts.โ€


8. ๐Ÿงฉ Resource Loading (async vs defer)

Definition:
Attributes that control how scripts load.

โœ… Key Points

  • async: downloads in parallel, executes ASAP (order not guaranteed).
  • defer: downloads in parallel, executes after HTML parse, preserves order.
  • Default: blocking (bad for performance).

๐ŸŽฏ Interview One-Liner

โ€œUse defer for scripts that rely on DOM, async for independent scripts. Default blocking scripts delay parsing and hurt performance.โ€


๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ Performance & Optimization


1. ๐Ÿงน Memory Leaks in Frontend

Definition:
A memory leak happens when memory is not released after itโ€™s no longer needed.

โœ… Common Sources

  • Uncleared timers/intervals:
  setInterval(() => { /* ... */ }, 1000); // never cleared
Enter fullscreen mode Exit fullscreen mode
  • Detached DOM nodes kept in closures or global arrays.
  • Event listeners not removed on unmount.
  • Global variables and singletons that grow unbounded.

๐ŸŽฏ Interview One-Liner

โ€œFrontend leaks often come from forgotten event listeners, timers, or DOM nodes retained in closures. Use cleanup (clearInterval, removeEventListener) and profiling tools.โ€


2. ๐Ÿ’ค Lazy Loading & Code Splitting

Definition:
Splitting bundles into smaller chunks and loading them on demand.

โœ… Key Points

  • Reduces initial load time.
  • Implemented with dynamic imports (import()), route-based splitting.
  • Use React.lazy + Suspense for components.

โš ๏ธ Gotchas

  • Too many small chunks โ†’ overhead.
  • Network latency can hurt if misconfigured.

๐ŸŽฏ Interview One-Liner

โ€œLazy loading reduces initial load by splitting code into chunks. Use import() or React.lazy. Balance chunk size to avoid too many round-trips.โ€


3. ๐ŸŒณ Tree-Shaking

Definition:
Removing unused code during bundling.

โœ… Key Points

  • Works only with ESM (import/export) because itโ€™s statically analyzable.
  • Dead code elimination depends on bundler + minifier (Webpack, Rollup, Terser).

โš ๏ธ Gotchas

  • Dynamic require() prevents tree-shaking.
  • Side-effects in modules can block elimination.

๐ŸŽฏ Interview One-Liner

โ€œTree-shaking eliminates unused exports but only works with static ES modules. Avoid dynamic imports and side-effects in libraries.โ€


4. โšก Web Vitals

Definition:
Googleโ€™s metrics for user-perceived performance.

โœ… Core Metrics

  • LCP (Largest Contentful Paint): load speed (<2.5s).
  • FID (First Input Delay): input responsiveness (<100ms).
  • CLS (Cumulative Layout Shift): visual stability (<0.1).
  • (New) INP (Interaction to Next Paint) replacing FID.

๐ŸŽฏ Interview One-Liner

โ€œWeb Vitals measure perceived performance: LCP for load, FID/INP for input, CLS for stability. Optimize via lazy loading, preloading, and reducing JS.โ€


5. ๐Ÿงพ Async Scheduling (Idle Time)

Definition:
Scheduling work when the browser is idle.

โœ… Key Points

  • requestIdleCallback(cb) โ†’ runs when browser is idle.
  • Useful for non-critical background work.
  • Fallback to timers for unsupported browsers.

โš ๏ธ Gotchas

  • Not guaranteed to run (if browser never idle).
  • Time budget limited (~50ms).

๐ŸŽฏ Interview One-Liner

โ€œrequestIdleCallback lets you run background work without blocking the main thread, but it may not fire on busy pages.โ€


6. ๐ŸŽฏ Preload, Prefetch, DNS Prefetch

โœ… Key Points

  • Preload: critical resources needed soon.
  • Prefetch: resources likely needed in future navigation.
  • DNS-prefetch: resolve domain names early.

Example

<link rel="preload" href="hero.jpg" as="image" />
<link rel="prefetch" href="/next-page.js" />
<link rel="dns-prefetch" href="//cdn.example.com" />
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œPreload fetches critical resources now, prefetch loads likely future ones, DNS-prefetch resolves domains early.โ€


7. ๐Ÿ“ฆ Bundling Strategies

  • Monolithic bundle โ†’ fast for small apps, bad for scale.
  • Code-splitting โ†’ route-level chunks.
  • Micro-frontends โ†’ separate bundles per domain (federation).
  • CDN caching โ†’ cache chunks by content hash.

๐ŸŽฏ Interview One-Liner

โ€œBundle strategy depends on scale. Use route-based code splitting, cache chunks with hashes, and avoid giant monolithic bundles.โ€


8. ๐ŸŽ๏ธ Rendering Performance Tips

  • Minimize reflows (batch DOM changes).
  • Use transform/opacity for animations.
  • Virtualize large lists (e.g., react-window).
  • Debounce resize/scroll listeners.

๐ŸŽฏ Interview One-Liner

โ€œReflows are expensive โ€” batch DOM updates, use transform/opacity for animations, and virtualize long lists.โ€


๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ Advanced Types & Equality


1. ๐Ÿ“Œ Truthy & Falsy Values

Definition:
When converted to a boolean (e.g., in if conditions), some values are considered truthy or falsy.

โœ… Falsy Values (only 7!)

  • false
  • 0, -0
  • "" (empty string)
  • null
  • undefined
  • NaN

Everything else โ†’ truthy (including "0", [], {}, Infinity).

โš ๏ธ Gotchas

if ("0") console.log("runs");   // runs (string "0" is truthy)
if ([]) console.log("runs");    // runs (empty array is truthy)
if ({}) console.log("runs");    // runs (empty object is truthy)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œOnly 7 values are falsy: false, 0, -0, "", null, undefined, NaN. Everything else is truthy โ€” even empty arrays/objects.โ€


2. ๐Ÿ”„ == vs ===

Definition:

  • === (strict equality): no type coercion.
  • == (loose equality): allows coercion.

โœ… Rules (selected weird ones)

0 == false        // true
"" == false       // true
null == undefined // true
[] == false       // true ( [] โ†’ "" โ†’ 0 )
[] == ![]         // true ( [] == false )
[1] == 1          // true ([1].toString() โ†’ "1")
[1,2] == "1,2"    // true
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Gotchas

  • NaN == NaN โ†’ false (NaN is never equal to itself).
  • null only equals undefined (and itself).
  • 0 == [] is true, but 0 == {} is false.

๐ŸŽฏ Interview One-Liner

โ€œ=== checks value + type, no coercion. == coerces and has weird rules: null == undefined only, arrays convert to strings, and NaN never equals itself.โ€


3. ๐Ÿงพ Special Numbers: NaN, Infinity, -0

โœ… NaN

  • Not equal to itself: NaN === NaN โ†’ false.
  • Use Number.isNaN() (better than isNaN).
  • isNaN("foo") โ†’ true (coerces to NaN).

โœ… Infinity

  • 1 / 0 โ†’ Infinity.
  • -1 / 0 โ†’ -Infinity.

โœ… -0

  • JS has two zeros: 0 and -0.
  • 0 === -0 โ†’ true.
  • But 1 / 0 === Infinity, 1 / -0 === -Infinity.

๐ŸŽฏ Interview One-Liner

โ€œJS has two zeros: +0 and -0. They compare equal with ===, but divide by them to get ยฑInfinity. NaN is never equal to itself.โ€


4. ๐Ÿ—๏ธ Object.is

Definition:
Like === but fixes edge cases:

โœ… Rules

Object.is(NaN, NaN);   // true
Object.is(0, -0);      // false
Object.is(5, 5);       // true
Object.is({}, {});     // false (different refs)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œObject.is is like strict equality but distinguishes -0 and +0, and treats NaN as equal to itself.โ€


5. ๐Ÿ“ฆ Primitive vs Object Wrappers

Definition:
Primitives (string, number, boolean, symbol, bigint, null, undefined) have object wrappers for method access.

โœ… Examples

"hello".toUpperCase();   // works because JS boxes into String object
new String("hello");     // object wrapper
typeof "hello";          // "string"
typeof new String("hi"); // "object"
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Gotchas

  • new Boolean(false) is truthy (object is always truthy).
  • Comparing primitive vs object:
"hi" === new String("hi"); // false
"hi" == new String("hi");  // true (coerces)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œPrimitives auto-box into wrappers (e.g., 'hi'.toUpperCase()). But wrapper objects (new String) are truthy and behave differently โ€” avoid them.โ€


6. ๐Ÿงฉ Typeof, instanceof, and Array.isArray

โœ… typeof

  • typeof null โ†’ "object" (bug since JS 1.0).
  • typeof NaN โ†’ "number".
  • typeof function() {} โ†’ "function".

โœ… instanceof

  • Checks prototype chain.
  • Works across inheritance.
  • Fails across iframes (different globals).

โœ… Array.isArray

  • Safest way to check arrays (cross-realm safe).

Example

[] instanceof Array;           // true
[] instanceof Object;          // true
Array.isArray([]);             // true
typeof null;                   // "object"
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œtypeof null is 'object' (legacy bug). instanceof checks prototype chains but breaks across realms. Use Array.isArray to check arrays safely.โ€


7. ๐Ÿงฎ Type Conversion (Explicit vs Implicit)

โœ… Explicit

  • Number("42") โ†’ 42
  • String(123) โ†’ "123"
  • Boolean(0) โ†’ false

โœ… Implicit (coercion)

  • 1 + "2" โ†’ "12" (string concatenation).
  • 1 - "2" โ†’ -1 (string โ†’ number).
  • [] + [] โ†’ "" (empty string).
  • [] + {} โ†’ "[object Object]".
  • {} + [] โ†’ 0 (parsed as block + array).

โš ๏ธ Gotchas

[] == ![]; // true
// [] -> "" -> 0, ![] -> false -> 0
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œType coercion is implicit conversion. Addition prefers strings, subtraction prefers numbers. Weird cases like [] + {} = '[object Object]' are from toString/valueOf conversions.โ€


๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ Error Handling & Debugging


1. โš ๏ธ try / catch / finally

Definition:
Mechanism for handling runtime exceptions.

โœ… Key Points

  • catch handles synchronous errors in the try block.
  • finally always executes (even if return/throw in try/catch).
  • Errors bubble up if unhandled.

โš ๏ธ Gotchas

try {
  throw new Error("boom");
} catch (e) {
  return "handled";
} finally {
  return "finally"; // overrides catch return
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œfinally always runs and overrides return values. Errors bubble up unless caught.โ€


2. ๐Ÿ•ธ๏ธ Error Types in JavaScript

โœ… Built-in Error Objects

  • Error โ†’ base class.
  • SyntaxError โ†’ invalid syntax (only at parse time).
  • ReferenceError โ†’ accessing undeclared variable.
  • TypeError โ†’ invalid operation (e.g., calling non-function).
  • RangeError โ†’ out-of-range numbers (e.g., invalid array length).
  • URIError โ†’ malformed URI in encodeURI/decodeURI.
  • EvalError โ†’ legacy, rarely used.

Example

try {
  JSON.parse("{ invalid }");
} catch (e) {
  console.log(e instanceof SyntaxError); // true
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œJS has typed errors: Syntax, Reference, Type, Range, URI. Most common in practice: TypeError and ReferenceError.โ€


3. ๐Ÿ› ๏ธ Custom Errors

Definition:
Extend the Error class for domain-specific errors.

Example

class ValidationError extends Error {
  constructor(message) {
    super(message);
    this.name = "ValidationError";
  }
}
throw new ValidationError("Invalid input");
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œCustom errors extend Error and set a name for better debugging.โ€


4. โšก Async Error Handling

โœ… Promises

fetch("bad-url")
  .then(res => res.json())
  .catch(err => console.error("Caught:", err));
Enter fullscreen mode Exit fullscreen mode
  • .catch() handles errors in promise chain.
  • Uncaught rejections trigger unhandledrejection event.

โœ… async/await

async function getData() {
  try {
    await fetch("bad-url");
  } catch (err) {
    console.error("Caught:", err);
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Must wrap await in try/catch.
  • Rejections not caught will bubble like exceptions.

๐ŸŽฏ Interview One-Liner

โ€œPromise errors bubble until caught. In async/await, use try/catch around awaits. Unhandled rejections trigger a global event.โ€


5. ๐ŸŒ Global Error Handling

โœ… Browser

window.onerror = (msg, url, line, col, err) => {
  console.error("Global error:", msg, err);
};
window.onunhandledrejection = e => {
  console.error("Unhandled rejection:", e.reason);
};
Enter fullscreen mode Exit fullscreen mode

โœ… Node.js

process.on("uncaughtException", err => { ... });
process.on("unhandledRejection", err => { ... });
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Gotchas

  • Donโ€™t rely on global handlers as the only mechanism โ€” theyโ€™re last resort.
  • Use for logging/alerting in production.

๐ŸŽฏ Interview One-Liner

โ€œGlobal error handlers (window.onerror, onunhandledrejection) are safety nets โ€” good for logging, but not a replacement for local handling.โ€


6. ๐Ÿงญ Debugging Techniques

โœ… Tools

  • console.* (log, warn, error, table, dir).
  • debugger keyword (pauses execution in DevTools).
  • Source maps (map minified code to original).

โœ… Advanced

  • Performance profiling (DevTools Performance tab).
  • Memory profiling (heap snapshots, allocation timelines).
  • Break on DOM mutation.

๐ŸŽฏ Interview One-Liner

โ€œUse console, debugger, and source maps for debugging. For perf/memory, use DevTools profiling and heap snapshots.โ€


7. ๐Ÿ“‹ Best Practices for Error Handling

  • Fail fast, fail safe โ†’ throw early, recover gracefully.
  • Donโ€™t swallow errors (catch(e){} with empty body).
  • Normalize errors (consistent structure across app).
  • Log with context (user action, environment).
  • Avoid throwing strings, always throw new Error(...).
  • Separate expected errors (validation) from unexpected ones (bugs).

๐ŸŽฏ Interview One-Liner

โ€œBest practice: never swallow errors, always throw Error objects, separate expected from unexpected, and log with context.โ€


๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ Concurrency & Parallelism


1. ๐Ÿงต Single-Threaded Model

Definition:
JavaScript executes on a single thread (the call stack) with concurrency simulated via the event loop.

โœ… Key Points

  • JS canโ€™t run two functions truly in parallel on the main thread.
  • Concurrency = overlapping tasks (via async callbacks).
  • Parallelism = tasks literally executing at the same time (needs threads/workers).

โš ๏ธ Gotchas

  • Long-running tasks block UI โ†’ โ€œfrozen pageโ€.
  • Async doesnโ€™t mean parallel (Promises still run on one thread).

๐ŸŽฏ Interview One-Liner

โ€œJavaScript is single-threaded; concurrency comes from the event loop, but real parallelism requires workers.โ€


2. โณ Concurrency via Event Loop

Definition:
The event loop interleaves tasks from macrotask & microtask queues.

โœ… Key Points

  • Concurrency = cooperative multitasking.
  • No two JS functions run at the same instant on the same thread.

Example

setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("promise"));
// Output: promise, timer
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œConcurrency in JS is managed by the event loop โ€” tasks interleave but donโ€™t run truly in parallel.โ€


3. โšก Parallelism via Web Workers

Definition:
Web Workers run JS in background threads separate from the main UI thread.

โœ… Key Points

  • Communicate via postMessage (structured cloning).
  • No direct DOM access.
  • Useful for CPU-heavy tasks.

โš ๏ธ Gotchas

  • Serialization overhead for large messages.
  • No shared state by default (copy-on-message).
  • Debugging workers is harder than main thread code.

Example

// worker.js
self.onmessage = e => self.postMessage(e.data * 2);

// main.js
const w = new Worker("worker.js");
w.onmessage = e => console.log(e.data);
w.postMessage(10); // 20
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œWeb Workers provide real parallelism by running JS in separate threads, but canโ€™t touch the DOM and communicate only via messages.โ€


4. ๐Ÿงฉ SharedArrayBuffer & Atomics

Definition:
APIs for shared memory and low-level synchronization between workers.

โœ… Key Points

  • SharedArrayBuffer: allows multiple threads to view/edit same memory.
  • Atomics: provides atomic operations (safe increments, waits, notifications).
  • Enables building locks, semaphores, and concurrent data structures in JS.

โš ๏ธ Gotchas

  • Very advanced โ€” rarely used directly, but underpins WebAssembly multithreading.
  • Security concerns โ†’ disabled after Spectre/Meltdown, re-enabled with stricter cross-origin isolation.

Example

const buffer = new SharedArrayBuffer(4);
const arr = new Int32Array(buffer);

Atomics.store(arr, 0, 42);
console.log(Atomics.load(arr, 0)); // 42
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œSharedArrayBuffer + Atomics let workers share memory and coordinate safely. Itโ€™s how JS supports real multithreading in WebAssembly.โ€


5. ๐Ÿงฎ Parallelism in Node.js

Definition:
Node is single-threaded per process, but offers parallelism.

โœ… Key Points

  • Worker Threads (since Node 10.5) โ†’ parallel JS execution.
  • Child Processes โ†’ true OS-level processes.
  • Cluster Module โ†’ multiple processes sharing load.
  • libuv threadpool โ†’ parallelize I/O and certain CPU tasks (crypto, fs).

๐ŸŽฏ Interview One-Liner

โ€œNode achieves parallelism with worker threads, child processes, or libuvโ€™s threadpool โ€” though the main JS thread is single.โ€


6. ๐ŸŽ๏ธ Practical Uses of Parallelism

  • Image processing in background via workers.
  • Large JSON parsing โ†’ offload to worker to avoid UI freeze.
  • Concurrent fetch requests โ†’ concurrency (not parallelism).
  • WebAssembly with threads โ†’ real CPU parallelism.

๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ ESNext & Modern Features


1. ๐Ÿ”ข BigInt

Definition:
A primitive type for arbitrarily large integers.

โœ… Key Points

  • Append n โ†’ 123n.
  • Canโ€™t mix with normal Number without explicit conversion.
  • No precision loss for huge numbers.

โš ๏ธ Gotchas

2n + 3; // TypeError (canโ€™t mix BigInt and Number)
Number(2n) === 2; // true
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œBigInt represents integers beyond Numberโ€™s 2^53-1 limit. You canโ€™t mix it with Number โ€” must explicitly convert.โ€


2. ๐Ÿงฉ Optional Chaining (?.)

Definition:
Safe property access without throwing if intermediate is null/undefined.

โœ… Key Points

  • Short-circuits if value is null/undefined.
  • Works with properties, function calls, array indexes.

Example

const user = {};
console.log(user.profile?.email); // undefined, not error
console.log(user.getName?.());    // undefined
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œOptional chaining avoids errors by short-circuiting on null/undefined. It works for props, calls, and arrays.โ€


3. ๐ŸŸฐ Nullish Coalescing (??)

Definition:
Provides a fallback only for null/undefined, not falsy values.

โœ… Key Points

0 || 42;   // 42
0 ?? 42;   // 0

"" || "x"; // "x"
"" ?? "x"; // ""
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œ?? differs from || by treating only null/undefined as missing. Falsy values like 0 and '' are kept.โ€


4. ๐Ÿ”„ Top-Level await

Definition:
Allows await at the top level of ES modules.

โœ… Key Points

  • Only allowed in ES modules (type="module").
  • Blocks module evaluation until awaited promise resolves.

Example

// top-level-await.js
const data = await fetch("/api").then(r => r.json());
console.log(data);
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œTop-level await lets you use await outside functions in ES modules, pausing module execution until resolved.โ€


5. โ™ป๏ธ WeakRefs & FinalizationRegistry

Definition:
APIs to reference objects without preventing GC.

โœ… WeakRef

  • new WeakRef(obj) โ†’ creates weak reference.
  • .deref() returns object if still alive, else undefined.

โœ… FinalizationRegistry

  • Lets you run cleanup after object GC (non-deterministic).

โš ๏ธ Gotchas

  • Unreliable for program logic โ€” GC is unpredictable.
  • Intended for caches, not control flow.

Example

let obj = { value: 123 };
const weak = new WeakRef(obj);

obj = null; // eligible for GC
console.log(weak.deref()); // maybe object, maybe undefined later
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œWeakRefs allow GCโ€™d objects to be referenced weakly, and FinalizationRegistry lets you clean them up. Use for caches, not program logic.โ€


6. ๐Ÿ“† Temporal API (Proposal Stage 3)

Definition:
New Date/Time API to replace Date (which is broken).

โœ… Key Points

  • Immutable, time zone aware, clear APIs.
  • Objects: PlainDate, PlainTime, ZonedDateTime, Duration.
  • Fixes DST bugs, leap second issues.

Example

import { Temporal } from "@js-temporal/polyfill";
const today = Temporal.Now.plainDateISO();
console.log(today.add({ days: 1 }).toString());
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œTemporal is the modern Date/Time API: immutable, time-zone aware, and accurate. Itโ€™s the future replacement for JS Date.โ€


7. ๐Ÿ“ฆ Dynamic Import

Definition:
import() loads modules dynamically at runtime.

โœ… Key Points

  • Returns a promise.
  • Enables conditional/lazy loading.
  • Used for code-splitting.

Example

if (condition) {
  const mod = await import("./math.js");
  mod.add(2, 3);
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œDynamic import lets you load modules at runtime. It returns a promise, enabling conditional and lazy loading.โ€


8. ๐ŸŽฏ Logical Assignment Operators

Definition:
Shorthand operators for logical + assignment.

โœ… Key Points

  • ||= assigns if falsy.
  • &&= assigns if truthy.
  • ??= assigns if null/undefined.

Example

let a = null;
a ||= 5;  // a = 5

let b = 0;
b ||= 5;  // b = 5
b ??= 5;  // b = 0 (unchanged)
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œLogical assignment operators (||=, &&=, ??=) combine short-circuiting with assignment. Handy for defaults and conditionals.โ€


9. ๐Ÿงพ Numeric Separators

Definition:
Underscores in numbers for readability.

Example

const big = 1_000_000_000;
console.log(big); // 1000000000
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œNumeric separators make large numbers readable, but donโ€™t affect the value.โ€


๐ŸŸฆ Core JavaScript โ€” Expert-Level Revision

๐ŸŸฆ Patterns & Architecture in JavaScript


1. ๐Ÿ“ฆ Module Pattern

Definition:
Encapsulates private state using closures, exposing only a public API.

โœ… Key Points

  • Common before ES6 modules (import/export).
  • Uses IIFEs (Immediately Invoked Function Expressions).
  • Prevents polluting global scope.

Example

const Counter = (function () {
  let count = 0; // private
  return {
    inc: () => ++count,
    get: () => count,
  };
})();
console.log(Counter.inc()); // 1
console.log(Counter.count); // undefined
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œThe module pattern hides implementation details with closures and exposes a controlled API. It was the precursor to ES modules.โ€


2. ๐Ÿ‘๏ธ Observer Pattern

Definition:
One-to-many dependency: when subject changes, observers are notified.

โœ… Key Points

  • Basis for event systems.
  • Used in RxJS, MobX, DOM events.

Example

class Subject {
  constructor() { this.observers = []; }
  subscribe(fn) { this.observers.push(fn); }
  notify(data) { this.observers.forEach(fn => fn(data)); }
}
const subject = new Subject();
subject.subscribe(data => console.log("Got:", data));
subject.notify("Hello");
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œThe observer pattern lets many listeners react when one subject changes. It underlies events, RxJS, and reactive libraries.โ€


3. ๐Ÿ“ก Pub/Sub Pattern

Definition:
Decouples publishers from subscribers via a mediator (event bus).

โœ… Key Points

  • Pub/Sub is like Observer but with a broker in between.
  • Subscribers donโ€™t know who published the event.

Example

const bus = {};
bus.events = {};
bus.subscribe = (event, fn) =>
  (bus.events[event] = (bus.events[event] || []).concat(fn));
bus.publish = (event, data) =>
  (bus.events[event] || []).forEach(fn => fn(data));

bus.subscribe("login", user => console.log("Welcome", user));
bus.publish("login", "Alice");
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œPub/Sub decouples producers and consumers with an event bus. Unlike Observer, subscribers donโ€™t directly attach to the subject.โ€


4. ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Singleton Pattern

Definition:
Restricts a class/module to a single instance.

โœ… Key Points

  • Common for global state, config, caches.
  • Enforced via closures or static variables.

Example

class Singleton {
  constructor() {
    if (Singleton.instance) return Singleton.instance;
    Singleton.instance = this;
  }
}
const a = new Singleton();
const b = new Singleton();
console.log(a === b); // true
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œSingleton ensures one instance globally โ€” useful for config, caches, or logging, but overuse makes testing harder.โ€


5. ๐Ÿญ Factory Pattern

Definition:
Function/class that creates objects without exposing the creation logic.

โœ… Key Points

  • Encapsulates instantiation.
  • Can return different subclasses depending on arguments.

Example

function createUser(type) {
  if (type === "admin") return { role: "admin" };
  return { role: "guest" };
}
console.log(createUser("admin"));
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œFactory abstracts object creation, returning different variants without exposing constructor details.โ€


6. ๐Ÿงฉ Prototype Pattern

Definition:
Creates new objects by cloning existing ones.

โœ… Key Points

  • In JS, all objects already inherit via prototypes.
  • Object cloning often uses Object.create(proto).

Example

const animal = { speak() { console.log("Hi"); } };
const dog = Object.create(animal);
dog.speak(); // "Hi"
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œPrototype pattern creates objects by cloning existing ones. In JavaScript, itโ€™s built into the language via Object.create.โ€


7. ๐Ÿงฎ Functional Patterns (FP in JS)

โœ… Immutability

  • Avoid mutating objects/arrays; use spread or Object.assign.

โœ… Function Composition

const compose = (f, g) => x => f(g(x));
const double = x => x * 2;
const square = x => x * x;
console.log(compose(square, double)(3)); // (3*2)^2 = 36
Enter fullscreen mode Exit fullscreen mode

โœ… Pipeline (proposed |> operator)

  • More readable left-to-right function chaining.

๐ŸŽฏ Interview One-Liner

โ€œFP patterns in JS emphasize immutability, pure functions, and composition. Composition allows small functions to build complex logic.โ€


8. โš–๏ธ Strategy Pattern

Definition:
Encapsulates interchangeable algorithms behind a common interface.

โœ… Example

class Payment {
  setStrategy(strategy) { this.strategy = strategy; }
  pay(amount) { this.strategy.pay(amount); }
}
class Paypal { pay(a) { console.log("PayPal:", a); } }
class Stripe { pay(a) { console.log("Stripe:", a); } }

const payment = new Payment();
payment.setStrategy(new Paypal());
payment.pay(100); // PayPal: 100
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œStrategy encapsulates interchangeable algorithms, letting you switch behaviors dynamically.โ€


9. ๐Ÿ•ต๏ธ Decorator Pattern

Definition:
Adds functionality to objects without modifying them.

โœ… Key Points

  • Popular in React (HOCs).
  • In ES, decorator syntax (@) is in proposal stage.

Example

function withLogging(fn) {
  return (...args) => {
    console.log("Args:", args);
    return fn(...args);
  };
}
const sum = (a, b) => a + b;
const loggedSum = withLogging(sum);
loggedSum(2, 3); // Logs args
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œDecorator adds behavior to functions or objects without modifying the original โ€” e.g., React HOCs.โ€


10. ๐Ÿ•ธ๏ธ Middleware Pattern

Definition:
Chain of functions where each step can process input and pass to next.

โœ… Key Points

  • Popular in Express.js, Redux.
  • Flexible for cross-cutting concerns (logging, auth).

Example

function compose(middleware) {
  return function (ctx) {
    let i = 0;
    function next() {
      const fn = middleware[i++];
      if (fn) fn(ctx, next);
    }
    next();
  };
}
const fn = compose([
  (ctx, next) => { ctx.push("a"); next(); },
  (ctx, next) => { ctx.push("b"); next(); }
]);
const arr = [];
fn(arr);
console.log(arr); // ["a","b"]
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Interview One-Liner

โ€œMiddleware composes a pipeline of functions where each can act and forward. Used in Express, Redux, Koa.โ€


โœ… Summary (Full Handbook)

This handbook covers expert-level JavaScript concepts in depth:

  • Closures, Scope, Hoisting, this, Prototypes, Event Loop
  • Asynchronous JavaScript (Timers, Promises, async/await, Generators, Debounce/Throttle, rAF, Workers)
  • Advanced Objects & Memory (GC, Symbols, WeakMap/WeakSet, Descriptors, Proxy/Reflect, Immutability)
  • Advanced Functions & ES6+ (HOFs, Currying, Modules, Destructuring, Params, Iterables, Map/Set, ES6+)
  • Browser & DOM (Event Delegation, Reflow vs Repaint, DOM APIs, Storage, Security, Service Workers, CRP, Loading)
  • Performance & Optimization (Leaks, Lazy Loading, Tree-shaking, Web Vitals, Async Scheduling, Bundling, Rendering)
  • Advanced Types & Equality (Truthy/Falsy, == vs ===, NaN/-0, Object.is, Wrappers, typeof quirks, Coercion)
  • Error Handling & Debugging (try/catch, Error types, Custom errors, Async handling, Global handlers, Debugging)
  • Concurrency & Parallelism (Single-threaded model, Event Loop, Workers, SharedArrayBuffer, Node parallelism)
  • ESNext & Modern Features (BigInt, Optional Chaining, Nullish, Top-level await, WeakRefs, Temporal, Imports, Operators)
  • Patterns & Architecture (Module, Observer, Pub/Sub, Singleton, Factory, Prototype, FP, Strategy, Decorator, Middleware)

Top comments (0)