JavaScript Interview Questions and Answers: Core to Advanced

JavaScript Interview Questions and Answers: Core to Advanced

Core JavaScript interview questions with short answers and runnable code, followed by advanced deep dives.

POSTED ON SEPTEMBER 19, 2025

This page covers the JavaScript interview questions that come up most often, from scope and closures to the event loop and promises. The core questions come first, each with a short answer and a runnable code example. After them, five advanced deep dives cover modules, arrow functions, generators, Proxy and Reflect, and testing async code.

Table of Contents

Core JavaScript Interview Questions
Advanced JavaScript Interview Questions
ES6 Modules vs CommonJS
Arrow Functions vs Regular Functions
Generators and Iterators
Proxy and Reflect APIs
Testing Asynchronous Code
Key Takeaways for Your Next Interview
Final Interview Tips

Core JavaScript Interview Questions

Each example runs as shown in a current Node.js LTS release.

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

var is function-scoped and can be redeclared, while let and const are block-scoped, so they only exist inside the block where they're declared. A let in a for loop header is scoped to the loop, with a fresh binding for each iteration. const stops you from reassigning the variable, but it doesn't make the value immutable. Use const by default and let when the value has to change.

for (var i = 0; i < 3; i++) {}
console.log(i);
// → 3

for (let j = 0; j < 3; j++) {}
console.log(typeof j);
// → undefined

const user = { name: "Ada" };
// allowed: changes the object
user.name = "Grace";
console.log(user.name);
// → Grace

try {
  // not allowed: reassignment
  user = {};
} catch (err) {
  console.log(err.name);
  // → TypeError
}

Common mistake: assuming const freezes an object. Its properties can still change, so use Object.freeze() if you need that.

2. What is hoisting, and what is the temporal dead zone?

Hoisting means JavaScript registers declarations before it runs the code in a scope. var declarations are hoisted and start as undefined, and function declarations are hoisted with their body, so you can call them before the line where they appear. let and const are hoisted too, but you can't use them until their declaration runs. That gap is the temporal dead zone (TDZ), and reading a variable inside it throws a ReferenceError.

console.log(a);
// → undefined

var a = 1;

console.log(greet());
// → hi

function greet() {
  return "hi";
}

try {
  console.log(b);
} catch (err) {
  console.log(err.name);
  // → ReferenceError
}
let b = 2;

Common mistake: saying let and const aren't hoisted. They are, which is why you get a TDZ error instead of a variable from an outer scope.

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

=== (strict equality) compares without any type conversion, so values of different types are never equal. == (loose equality) follows the Abstract Equality rules: null and undefined are loosely equal to each other and to nothing else, and otherwise values of different types are converted, usually to numbers, before they're compared. That's why "" == 0 is true: the empty string converts to 0. Use === by default; a common exception is value == null, which checks for both null and undefined.

console.log(0 == "0");
// → true

console.log("" == 0);
// → true

console.log(0 === "0");
// → false

console.log(null == undefined);
// → true

console.log(null === undefined);
// → false

console.log(NaN === NaN);
// → false

Common mistake: checking for NaN with ===. NaN isn't equal to itself, so use Number.isNaN(value).

4. What is the difference between null and undefined?

undefined means no value was assigned: a declared but uninitialized variable, a missing object property, or the result of a function that returns nothing. null is a value you assign on purpose to say "there is no value here". typeof null returns "object", an old quirk the language keeps for backward compatibility.

let notSet;
console.log(notSet);
// → undefined

console.log({}.missing);
// → undefined

let cleared = null;
console.log(cleared);
// → null

console.log(typeof undefined);
// → undefined

console.log(typeof null);
// → object

console.log(0 || 10);
// → 10

console.log(0 ?? 10);
// → 0

Common mistake: using || for default values, which also replaces 0 and "". Use ?? when only null and undefined should get the default.

5. What is a closure?

A closure is a function that keeps access to the variables of the scope where it was created, even after the outer function has returned. Closures give you private state, and they power callbacks, event handlers, and helpers like debounce. Each call to the outer function creates a new, separate scope.

function makeCounter() {
  let count = 0;
  return () => {
    count++;
    return count;
  };
}

const counter = makeCounter();
console.log(counter());
// → 1

console.log(counter());
// → 2

const other = makeCounter();
console.log(other());
// → 1

Common mistake: creating callbacks in a for loop that uses var. All callbacks share one variable and see its final value, while let creates a new variable for each iteration.

6. How is the value of this determined?

this depends on how a function is called, not where it's defined. In a method call like obj.fn(), this is obj; in a plain call, it's globalThis in non-strict (sloppy) code and undefined in strict mode and modules; with new, it's the new object; and call, apply, and bind set it explicitly. Arrow functions don't have their own this and use the one from the surrounding scope.

"use strict";

const user = {
  name: "Ada",
  greet() {
    return `Hi, ${this.name}`;
  },
};

console.log(user.greet());
// → Hi, Ada

const greet = user.greet;
try {
  // plain call: this is
  // undefined, so it throws
  greet();
} catch (err) {
  console.log(err.name);
  // → TypeError
}

Common mistake: passing a method as a callback, as in setTimeout(user.greet, 100). The method loses its object, so bind it or wrap it in an arrow function.

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

All three set this for a function. call runs the function right away with arguments listed one by one, and apply runs it right away with the arguments in an array. bind doesn't run anything; it returns a new function with this, and optionally the first arguments, fixed.

function intro(hi, end) {
  const name = this.name;
  return `${hi}, ${name}${end}`;
}

const ada = { name: "Ada" };

console.log(
  intro.call(ada, "Hi", "!"),
);
// → Hi, Ada!

console.log(
  intro.apply(ada, ["Yo", "?"]),
);
// → Yo, Ada?

const helloAda = intro.bind(
  ada,
  "Hello",
);
console.log(helloAda("."));
// → Hello, Ada.

Common mistake: expecting bind to call the function. It only returns a new one, which you still have to call.

8. How does prototypal inheritance work?

Every object has an internal link to its prototype, which is another object or null (as with Object.create(null)). When you read a property the object doesn't have, JavaScript looks for it along this prototype chain. Classes use the same mechanism: instance methods declared in a class body live on the class's prototype and are shared by all instances, while static methods live on the class itself.

const pet = {
  speak() {
    return `I'm ${this.name}`;
  },
};

const dog = Object.create(pet);
dog.name = "Rex";

console.log(dog.speak());
// → I'm Rex

console.log(
  Object.hasOwn(dog, "speak"),
);
// → false

const proto =
  Object.getPrototypeOf(dog);
console.log(proto === pet);
// → true

Common mistake: treating class as a separate inheritance model. It's syntax over prototypes, so extends still links prototype chains.

9. How does the event loop work?

JavaScript runs your code on a single thread with one call stack. Async callbacks wait in queues: promise callbacks, including code after await, go to the microtask queue, while setTimeout, I/O, and UI events go to the macrotask (task) queue. Each time the stack is empty, the event loop runs every pending microtask before it takes the next macrotask.

console.log("1: sync");

setTimeout(() => {
  console.log("4: timeout");
}, 0);

Promise.resolve().then(() => {
  console.log("3: microtask");
});

console.log("2: sync");

// Prints in order:
// → 1: sync
// → 2: sync
// → 3: microtask
// → 4: timeout

Common mistake: expecting setTimeout(fn, 0) to run right away. It runs only after the current code and all pending microtasks have finished.

10. How do promises and async/await work, and how do you handle errors?

A promise represents a value that will be ready later: it starts pending and then becomes fulfilled or rejected. An async function always returns a promise, and await pauses that function until a promise settles, so async code reads from top to bottom. Catch errors with try...catch around await, or with .catch() at the end of a promise chain.

const wait = (ms) =>
  new Promise((resolve) => {
    setTimeout(resolve, ms);
  });

async function getUser(id) {
  await wait(100);
  if (id <= 0) {
    // rejects the promise
    throw new Error("Bad id");
  }
  return { id, name: "Ada" };
}

async function showUser(id) {
  try {
    const user =
      await getUser(id);
    console.log(user.name);
  } catch (err) {
    console.log(
      `Failed: ${err.message}`,
    );
  }
}

showUser(1);
// → Ada

showUser(-1);
// → Failed: Bad id

Common mistake: using await inside forEach. forEach doesn't wait for promises, so use a for...of loop, or Promise.all() to run the calls in parallel.

11. What is the difference between a shallow copy and a deep copy?

A shallow copy creates a new top-level object, but nested objects and arrays are still shared with the original. Spread syntax and Object.assign() make shallow copies. structuredClone() makes a deep copy, including nested objects, arrays, Map, Set, and Date values.

const original = {
  name: "Ada",
  skills: ["js"],
};

const shallow = { ...original };
shallow.skills.push("css");
console.log(original.skills);
// → [ 'js', 'css' ]

const deep =
  structuredClone(original);
deep.skills.push("sql");
console.log(original.skills);
// → [ 'js', 'css' ]

console.log(deep.skills);
// → [ 'js', 'css', 'sql' ]

Common mistake: deep copying with JSON.parse(JSON.stringify(obj)). It drops functions and undefined values and turns dates into strings. structuredClone() has limits too: it throws a DataCloneError on functions, and class instances come back as plain objects without their prototype.

12. What do map, filter, and reduce do?

All three are array methods that return a new value without changing the original array. map transforms each element and returns an array of the same length, and filter returns only the elements that pass a test. reduce combines all elements into a single value, such as a sum or an object.

const prices = [5, 12, 8, 20];

const doubled = prices.map(
  (p) => p * 2,
);
console.log(doubled);
// → [ 10, 24, 16, 40 ]

const over10 = prices.filter(
  (p) => p > 10,
);
console.log(over10);
// → [ 12, 20 ]

const total = prices.reduce(
  (sum, p) => sum + p,
  0,
);
console.log(total);
// → 45

Common mistake: leaving out the initial value in reduce. On an empty array, that throws a TypeError.

13. What are destructuring, spread, and rest?

Destructuring unpacks array items or object properties into variables, with optional defaults. Spread (...) expands an array or object into separate items or properties, for example to pass arguments or copy and merge objects. Rest uses the same ... syntax to collect the remaining items, properties, or arguments into a new array or object. Spread appears where values are used, and rest appears where they're received.

const user = {
  name: "Ada",
  age: 36,
  city: "Oslo",
};

// destructuring + object rest
const { name, ...rest } = user;
console.log(name);
// → Ada

console.log(rest);
// → { age: 36, city: 'Oslo' }

// rest collects the arguments
function sum(...nums) {
  return nums.reduce(
    (a, b) => a + b,
    0,
  );
}

// spread passes array items
const scores = [1, 2, 3];
console.log(sum(...scores));
// → 6

Common mistake: destructuring a missing argument, as in function f({ id }) {} called as f(). Destructuring undefined throws a TypeError, so give the parameter a default: function f({ id } = {}) {}.

14. What is the difference between primitives and objects (value vs reference)?

Primitives (strings, numbers, booleans, null, undefined, symbols, and bigints) are immutable, and assigning one to another variable copies the value. Objects, including arrays and functions, are handled by reference: assigning or passing an object copies the reference, so both variables point to the same object. That's also why === compares objects by identity, not by their contents.

// primitives are copied
let a = 1;
let b = a;
b = 2;
console.log(a);
// → 1

// objects share a reference
const x = { count: 1 };
const y = x;
y.count = 2;
console.log(x.count);
// → 2

console.log(x === y);
// → true

console.log([1] === [1]);
// → false

Common mistake: expecting a function to replace the caller's object by reassigning its parameter. That only changes the local variable, while changing a property of the parameter changes the shared object.


Advanced JavaScript Interview Questions

The next five questions go deeper, with the context and trade-offs interviewers expect in mid-level and senior interviews.

ES6 Modules vs CommonJS

This question tests your understanding of JavaScript’s module systems and when to use each approach. It’s one of the most common questions for mid to senior-level positions.

Why This Question Matters

Module systems are fundamental to modern JavaScript development. Understanding the differences between ES6 modules and CommonJS affects bundling strategies, performance optimization, and architectural decisions. This question reveals whether you understand the practical implications of different module approaches.

You may also get follow-up questions about variable declarations like var, let, and const, or how modules behave under strict mode, which is enabled by default in ES modules.

The Complete Answer Framework

Start with the core distinction: ES6 modules use import/export syntax and are statically analyzed, while CommonJS uses require()/module.exports and loads modules dynamically at runtime.

Basic Syntax Comparison

First, let’s look at how each system handles exports and imports:

ES6 Modules:

// Named exports
export const PI = 3.14159;
export function add(a, b) {
  return a + b;
}

// Default export
export default function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

// Importing
import factorial, { PI, add } from './mathModule.js';
import * as math from './mathModule.js';

CommonJS:

// Exporting
const PI = 3.14159;
function add(a, b) {
  return a + b;
}

module.exports = {
  PI,
  add,
  factorial: function factorial(n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
  }
};

// Importing
const { PI, add } = require('./mathModule');
const math = require('./mathModule');

Critical Differences You Must Know

1. Static vs Dynamic Analysis

ES6 modules are analyzed at compile time, enabling better tooling and optimization:

// ES6 - Static analysis allows tree-shaking
import { usedFunction } from './utils.js';
// Bundlers can drop the unused exports

// CommonJS - Dynamic nature limits tree-shaking
const { usedFunction } = require('./utils');
// Unused functions usually end up in the bundle

This difference directly impacts bundle size and performance in production applications.

2. Live Bindings vs Copies

This is a crucial distinction that often trips up candidates:

// ES6 modules provide live bindings
// counter.js
let counter = 0;
export function increment() { counter++; }
export { counter };

// main.js
import { counter, increment } from './counter.js';
console.log(counter); // 0
increment();
console.log(counter); // 1 (live binding updated!)

// CommonJS shares the exports object, but primitives are snapshots
// counter.js
let counter = 0;
module.exports = {
  counter: counter, // Value copied at export time
  increment: () => { counter++; },
  getCounter: () => counter
};

// main.js
const { counter, increment, getCounter } = require('./counter');
console.log(counter); // 0
increment();
console.log(counter); // Still 0 (copy doesn't update)
console.log(getCounter()); // 1 (function call gets current value)

Modern Development Implications

Tree Shaking and Bundle Optimization

ES6 modules enable sophisticated build optimizations that directly impact user experience:

// Large utility library
export function criticalFunction() { /* used everywhere */ }
export function rarelyUsedFunction() { /* used in one place */ }
export function debugFunction() { /* only for development */ }

// Your application
import { criticalFunction } from './utils.js';

// A tree-shaking bundler can remove unused code
// Result: Smaller bundle, faster load times

Dynamic Imports for Code Splitting

Modern applications use dynamic imports for performance optimization:

// Load features only when needed
async function loadAdvancedFeatures() {
  const { AdvancedChart } = await import('./advanced-charts.js');
  return new AdvancedChart();
}

// Conditional loading based on user actions
if (userClickedAdvancedMode) {
  const advancedModule = await import('./advanced-features.js');
  advancedModule.initialize();
}

CommonJS code can call import() too. A require() inside a function can also defer loading, but it's synchronous, so bundlers can't split it into a separately downloaded chunk the way they can with import(). The pattern is essential for modern web applications.

When to Use Each System

Choose ES6 modules when:

  • Building for modern browsers or using a bundler
  • You want tree-shaking for smaller bundle sizes
  • Working with static analysis tools
  • Building reusable component libraries
  • Implementing code splitting strategies

Stick with CommonJS when:

  • Working in Node.js environments without ES module support
  • Dealing with legacy codebases
  • Need conditional loading based on runtime conditions
  • Working with packages that don’t support ES modules

Interoperability Strategies

Modern projects often need to handle both systems:

// package.json for dual compatibility
{
  "main": "./lib/commonjs/index.js",
  "module": "./lib/esm/index.js",
  "exports": {
    ".": {
      "import": "./lib/esm/index.js",
      "require": "./lib/commonjs/index.js"
    }
  }
}

Key Points Summary

  • Static analysis: ES6 modules enable better tooling and optimization
  • Live bindings: ES6 imports are live bindings, while CommonJS has none: a value copied out of module.exports (for example, by destructuring) won't see later changes to the module's local variable, and only reading a property of the shared exports object (like mod.counter) sees an update if the module reassigns that property
  • Tree shaking: Works best with ES6 modules, depending on the bundler
  • Performance: ES6 modules support code splitting and lazy loading
  • Compatibility: Consider your target environment and build tools

What Interviewers Want to Hear

They’re looking for understanding of practical implications like bundle optimization, performance considerations, and architectural decisions. Mention specific scenarios where you’ve chosen one over the other and why.


Arrow Functions vs Regular Functions

This question assesses your understanding of JavaScript’s this keyword, function behavior, and when to choose different function types.

You might also be asked to demonstrate a function expression or a callback function that uses an arrow function—common patterns for modern async code.

Why This Question Matters

Arrow functions aren’t just syntactic sugar – they fundamentally change how this works in JavaScript. Understanding these differences is crucial for avoiding bugs in event handlers, class methods, and functional programming patterns. This question reveals whether you understand JavaScript’s execution context and can make informed decisions about function types.

Knowing where each function operates—inside block scope, function scope, or global scope—is critical for debugging and writing predictable code.

The Complete Answer Framework

Lead with the most important difference: Arrow functions don’t have their own this context – they inherit it lexically from the enclosing scope.

The this Binding Challenge

This is the most critical difference and the source of many JavaScript bugs:

class EventHandler {
  constructor() {
    this.clickCount = 0;
    this.setupEventListeners();
  }

  setupEventListeners() {
    // Arrow function preserves 'this' context
    document.getElementById('button').addEventListener('click', () => {
      this.clickCount++; // 'this' refers to EventHandler instance
      this.updateDisplay();
    });

    // Regular function loses 'this' context
    document.getElementById('other-button').addEventListener('click', function() {
      console.log(this.clickCount); // undefined! 'this' refers to button element
    });
  }

  updateDisplay() {
    console.log(`Clicked ${this.clickCount} times`);
  }
}

This example demonstrates why arrow functions became essential for modern JavaScript development.

Constructor and Prototype Differences

Understanding why arrow functions can’t be constructors reveals deeper JavaScript concepts:

// Regular functions can be constructors
function UserCreator(name) {
  this.name = name;
  this.greet = function() {
    return `Hello, I'm ${this.name}`;
  };
}

UserCreator.prototype.sayGoodbye = function() {
  return `Goodbye from ${this.name}`;
};

const user = new UserCreator('Alice');

// Arrow functions cannot be constructors
const ArrowCreator = (name) => {
  this.name = name; // This won't work as expected
};

try {
  const user2 = new ArrowCreator('Bob'); // TypeError!
} catch (error) {
  console.log('Cannot use arrow function as constructor');
}

// Arrow functions don't have prototype
console.log(UserCreator.prototype); // Object with constructor property
console.log(ArrowCreator.prototype); // undefined

Arguments Object vs Rest Parameters

Modern JavaScript favors rest parameters over the arguments object:

// Regular functions have 'arguments' object
function oldStyleFunction() {
  console.log('Arguments:', Array.from(arguments));
  console.log('First argument:', arguments[0]);
}

// Arrow functions use rest parameters
const modernFunction = (...args) => {
  console.log('Arguments:', args);
  console.log('First argument:', args[0]);
};

oldStyleFunction('a', 'b', 'c');
modernFunction('x', 'y', 'z');

Rest parameters are more explicit and work with array methods directly.

Practical Use Cases

Functional Programming Patterns

Arrow functions excel in functional programming scenarios:

const users = [
  { name: 'Alice', age: 25, active: true },
  { name: 'Bob', age: 30, active: false },
  { name: 'Carol', age: 35, active: true }
];

// Clean, readable functional chains
const activeAdultNames = users
  .filter(user => user.active)
  .filter(user => user.age >= 18)
  .map(user => user.name.toUpperCase())
  .sort((a, b) => a.localeCompare(b));

console.log(activeAdultNames); // ['ALICE', 'CAROL']

Promise Chains and Async Operations

Arrow functions maintain context through async operations:

class ApiService {
  constructor(baseUrl) {
    this.baseUrl = baseUrl;
    this.cache = new Map();
  }

  async fetchUserData(userId) {
    const url = `${this.baseUrl}/users/${userId}`;
    
    try {
      const response = await fetch(url);
      const userData = await response.json();
      
      // Arrow functions preserve 'this' context
      const processedData = userData
        .filter(item => item.active)
        .map(item => ({
          ...item,
          processedAt: new Date().toISOString()
        }));

      return processedData;
    } catch (error) {
      console.error(`${this.constructor.name} error:`, error.message);
      throw error;
    }
  }
}

Higher-Order Functions and Currying

Arrow functions create clean, composable functions:

// Function composition with arrow functions
const pipe = (...functions) => (value) =>
  functions.reduce((acc, fn) => fn(acc), value);

const addOne = x => x + 1;
const multiplyByTwo = x => x * 2;
const subtractThree = x => x - 3;

const complexOperation = pipe(addOne, multiplyByTwo, subtractThree);
console.log(complexOperation(5)); // ((5 + 1) * 2) - 3 = 9

// Partial application
const createLogger = prefix => level => message =>
  `[${prefix}] ${level.toUpperCase()}: ${message}`;

const appLogger = createLogger('MyApp');
const infoLogger = appLogger('info');

console.log(infoLogger('Application started'));

Performance Considerations

Understanding the memory implications helps make informed decisions:

class PerformanceExample {
  // Arrow method creates new function per instance
  arrowMethod = () => {
    return 'arrow method';
  }

  // Regular method shared across all instances
  regularMethod() {
    return 'regular method';
  }
}

const instance1 = new PerformanceExample();
const instance2 = new PerformanceExample();

// Different function objects
console.log(instance1.arrowMethod === instance2.arrowMethod); // false

// Same function object
console.log(instance1.regularMethod === instance2.regularMethod); // true

For classes with many instances, regular methods are more memory-efficient.

Decision Framework

Use arrow functions for:

  • Event handlers in classes (preserves this)
  • Array methods (map, filter, reduce)
  • Promise chains and async operations
  • Functional programming patterns
  • Short utility functions
  • Callbacks that need outer scope’s this

Use regular functions for:

  • Constructor functions
  • Methods in object literals (usually)
  • When you need the arguments object
  • When function should be hoisted
  • Methods that need dynamic this binding
  • Performance-critical code with many instances

Key Points Summary

  • this binding: Arrow functions inherit lexically, regular functions bind dynamically
  • Constructors: Only regular functions can be constructors
  • Arguments: Arrow functions use rest parameters, regular functions have arguments
  • Hoisting: Regular function declarations are hoisted, arrow functions are not
  • Performance: Consider memory usage in classes with many instances

What Interviewers Want to Hear

They want to see that you understand the this binding implications and can choose the right function type for different scenarios. Mention specific use cases where each type works better and performance considerations.


Generators and Iterators

This question separates candidates who know syntax from those who understand practical applications and advanced async patterns.

Why This Question Matters

Generators represent a fundamental shift in how JavaScript handles sequences and asynchronous operations. They enable lazy evaluation, memory-efficient data processing, and elegant solutions to complex iteration problems.

Generators rely on functions that pause and resume execution. They work closely with for loop syntax and help process sequences efficiently.

You can think of generators as advanced tools for handling data structures like arrays and maps, often returning key-value pairs one at a time.

Plain generators are synchronous, but async generators pair with the event loop through for await...of, which ties them to one of the trickiest interview topics.

Some interviewers may compare generators to closures: a paused generator keeps its local variables until the next next() call, much like a closure keeps its scope.

For performance-focused scenarios, pairing lazy iteration with memoization can reduce repeated work across iterations.

This question tests whether you understand advanced control flow and can identify scenarios where generators provide significant benefits.

The Complete Answer Framework

Start with the fundamental concept: Generators are special functions that can pause and resume their execution, yielding values one at a time using the yield keyword. They implement the iterator protocol automatically.

Basic Generator Concepts

Understanding the execution model is crucial:

function* simpleGenerator() {
  console.log('Generator started');
  yield 1;
  console.log('After first yield');
  yield 2;
  console.log('After second yield');
  yield 3;
  console.log('Generator finished');
}

const gen = simpleGenerator();
console.log('Generator created, but not started yet');

console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: 3, done: false }
console.log(gen.next()); // { value: undefined, done: true }

This demonstrates that generators are lazy – they only execute when values are requested.

Powerful Real-World Applications

Memory-Efficient Data Processing

Generators solve memory problems with large datasets:

function* processLargeDataset(data) {
  for (let i = 0; i < data.length; i += 1000) {
    const batch = data.slice(i, i + 1000);
    
    for (const item of batch) {
      if (item.isValid) {
        yield {
          id: item.id,
          processedAt: new Date(),
          result: performExpensiveOperation(item)
        };
      }
    }
  }
}

// Process millions of records without building a second results array
const millionRecords = createLargeDataset();
const processor = processLargeDataset(millionRecords);

for (const result of processor) {
  saveToDatabase(result);
  if (shouldStop()) break; // Can stop processing early
}

The input array is still fully loaded, but the processing step uses constant extra memory regardless of dataset size.

Infinite Sequences

Generators naturally represent infinite sequences:

function* fibonacci() {
  let [prev, curr] = [0, 1];
  while (true) {
    yield curr;
    [prev, curr] = [curr, prev + curr];
  }
}

function* primes() {
  const found = [];
  for (let candidate = 2; ; candidate++) {
    // Trial division against the primes found so far
    if (found.every(p => candidate % p !== 0)) {
      found.push(candidate);
      yield candidate;
    }
  }
}

// Take only what you need
// take() is defined in Generator Utilities below
const first10Primes = take(primes(), 10);
console.log([...first10Primes]); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Tree Traversal Algorithms

Generators make tree traversal elegant and flexible:

class TreeNode {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }

  * inOrder() {
    if (this.left) yield* this.left.inOrder();
    yield this.value;
    if (this.right) yield* this.right.inOrder();
  }

  * breadthFirst() {
    const queue = [this];
    while (queue.length > 0) {
      const node = queue.shift();
      yield node.value;
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
}

const tree = new TreeNode(4,
  new TreeNode(2, new TreeNode(1), new TreeNode(3)),
  new TreeNode(6, new TreeNode(5), new TreeNode(7))
);

console.log('In-order:', [...tree.inOrder()]); // [1, 2, 3, 4, 5, 6, 7]
console.log('Breadth-first:', [...tree.breadthFirst()]); // [4, 2, 6, 1, 3, 5, 7]

Async Generators for Modern APIs

Async generators handle paginated APIs elegantly:

async function* fetchAllPages(apiUrl) {
  let page = 1;
  let hasMore = true;

  while (hasMore) {
    const response = await fetch(`${apiUrl}?page=${page}&limit=100`);
    const data = await response.json();
    
    for (const item of data.items) {
      yield item;
    }
    
    hasMore = data.hasNextPage;
    page++;
  }
}

// Clean consumption of all paginated data
async function processAllUsers() {
  for await (const user of fetchAllPages('/api/users')) {
    await processUser(user);
    // Automatically handles pagination behind the scenes
  }
}

This pattern is invaluable for APIs with thousands of records.

Generator Utilities and Composition

Build reusable generator utilities:

function* take(iterable, count) {
  if (count <= 0) return;
  let taken = 0;
  for (const item of iterable) {
    yield item;
    if (++taken >= count) return;
  }
}

function* filter(iterable, predicate) {
  for (const item of iterable) {
    if (predicate(item)) yield item;
  }
}

function* map(iterable, transform) {
  for (const item of iterable) {
    yield transform(item);
  }
}

function* chain(...iterables) {
  for (const iterable of iterables) {
    yield* iterable;
  }
}

function* range(start, end) {
  for (let i = start; i < end; i++) yield i;
}

// Compose operations efficiently
const evenSquares = map(
  filter(range(1, 1000), x => x % 2 === 0),
  x => x * x
);

console.log([...take(evenSquares, 5)]); // [4, 16, 36, 64, 100]

Advanced Patterns

State Machines

Generators naturally implement state machines:

function* stateMachine() {
  let state = 'idle';
  let data = null;

  while (true) {
    const input = yield { state, data };

    switch (state) {
      case 'idle':
        if (input?.action === 'start') {
          state = 'processing';
          data = { started: new Date(), items: [] };
        }
        break;

      case 'processing':
        if (input?.action === 'add') {
          data.items.push(input.item);
        } else if (input?.action === 'finish') {
          state = 'completed';
          data.finished = new Date();
        }
        break;

      case 'completed':
        if (input?.action === 'reset') {
          state = 'idle';
          data = null;
        }
        break;
    }
  }
}

const machine = stateMachine();
console.log(machine.next().value); // { state: 'idle', data: null }
console.log(machine.next({ action: 'start' }).value); // { state: 'processing', ... }

Backtracking Algorithms

Generators excel at backtracking problems:

function* solveNQueens(n) {
  const board = Array(n).fill().map(() => Array(n).fill(false));
  
  function* placeQueens(row) {
    if (row === n) {
      yield board.map(row => [...row]); // Found solution
      return;
    }

    for (let col = 0; col < n; col++) {
      if (isSafe(row, col)) {
        board[row][col] = true;
        yield* placeQueens(row + 1);
        board[row][col] = false; // Backtrack
      }
    }
  }

  function isSafe(row, col) {
    // Check column and diagonals
    for (let i = 0; i < row; i++) {
      if (board[i][col] || 
          (col - (row - i) >= 0 && board[i][col - (row - i)]) ||
          (col + (row - i) < n && board[i][col + (row - i)])) {
        return false;
      }
    }
    return true;
  }

  yield* placeQueens(0);
}

// Find all solutions to 4-Queens
const solutions = [...solveNQueens(4)];
console.log(`Found ${solutions.length} solutions`);

The Iterator Protocol

Understanding the underlying protocol helps with custom implementations:

class NumberRange {
  constructor(start, end, step = 1) {
    this.start = start;
    this.end = end;
    this.step = step;
  }

  [Symbol.iterator]() {
    let current = this.start;
    const end = this.end;
    const step = this.step;

    return {
      next() {
        if (current < end) {
          const value = current;
          current += step;
          return { value, done: false };
        }
        return { value: undefined, done: true };
      }
    };
  }
}

const range = new NumberRange(1, 10, 2);
console.log([...range]); // [1, 3, 5, 7, 9]

Key Points Summary

  • Lazy evaluation: Values generated only when requested
  • Memory efficiency: Process large datasets without loading everything
  • Natural iteration: Clean syntax for complex iteration patterns
  • Async support: Perfect for paginated APIs and async sequences
  • Composability: Build complex operations from simple generators

What Interviewers Want to Hear

Focus on scenarios where generators solve real problems: handling large datasets, implementing infinite sequences, clean async iteration, memory-conscious algorithms, and state machines. Mention specific performance benefits and when you’d choose generators over alternatives.


Proxy and Reflect APIs

This advanced question tests understanding of JavaScript’s introspection capabilities and meta-programming patterns.

Why This Question Matters

Proxy and Reflect represent JavaScript’s meta-programming capabilities – the ability to intercept and customize fundamental operations like property access, assignment, and function calls. These APIs enable sophisticated patterns like transparent debugging, property validation, and dynamic API creation. This question reveals whether you understand advanced JavaScript concepts and can implement elegant solutions to complex problems.

For example, you could use them to detect when a value becomes NaN (not a number) or when a property is deleted.

You might also combine these APIs with a spread operator to clone or merge objects efficiently, or with a new array operation to handle collections more elegantly.

These techniques align well with classic design patterns, such as Proxy, Decorator, and Observer, when structuring dynamic behavior.

The Complete Answer Framework

Define the concepts clearly: Proxy lets you intercept and customize operations on objects (property access, assignment, function calls, etc.), while Reflect provides methods to perform these operations programmatically with a consistent API.

Essential Proxy Traps

Understanding the available traps is fundamental:

const comprehensiveProxy = new Proxy(target, {
  get(target, property, receiver) {
    // Intercept property access
    console.log(`Getting ${property}`);
    return Reflect.get(target, property, receiver);
  },

  set(target, property, value, receiver) {
    // Intercept property assignment
    console.log(`Setting ${property} = ${value}`);
    return Reflect.set(target, property, value, receiver);
  },

  has(target, property) {
    // Intercept 'in' operator
    console.log(`Checking if ${property} exists`);
    return Reflect.has(target, property);
  },

  deleteProperty(target, property) {
    // Intercept 'delete' operator
    console.log(`Deleting ${property}`);
    return Reflect.deleteProperty(target, property);
  },

  ownKeys(target) {
    // Intercept Object.keys(), for...in, etc.
    console.log(`Getting own keys`);
    return Reflect.ownKeys(target);
  }
});

Powerful Real-World Applications

Property Validation System

Create objects with built-in validation:

function createValidatedObject(schema) {
  const data = {};
  
  return new Proxy(data, {
    set(target, property, value, receiver) {
      const validator = schema[property];
      
      if (validator) {
        const result = validator(value);
        if (result !== true) {
          throw new Error(`Validation failed for ${property}: ${result}`);
        }
      }
      
      return Reflect.set(target, property, value, receiver);
    },

    get(target, property, receiver) {
      // Add special methods
      if (property === 'isValid') {
        return () => {
          for (const [prop, validator] of Object.entries(schema)) {
            if (prop in target && validator(target[prop]) !== true) {
              return false;
            }
          }
          return true;
        };
      }
      
      return Reflect.get(target, property, receiver);
    }
  });
}

const userSchema = {
  name: value => typeof value === 'string' && value.length > 0 || 'Name required',
  age: value => Number.isInteger(value) && value > 0 || 'Valid age required',
  email: value => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) || 'Valid email required'
};

const validatedUser = createValidatedObject(userSchema);
validatedUser.name = 'Alice';
validatedUser.age = 25;
validatedUser.email = 'alice@example.com';

console.log('Is valid:', validatedUser.isValid()); // true

Virtual Objects with Computed Properties

Create objects with dynamic, computed properties:

function createVirtualObject(computeFunctions) {
  return new Proxy({}, {
    get(target, property) {
      if (Object.hasOwn(computeFunctions, property)) {
        return computeFunctions[property]();
      }
      return Reflect.get(target, property);
    },

    has(target, property) {
      return Object.hasOwn(computeFunctions, property) || Reflect.has(target, property);
    },

    ownKeys(target) {
      return [...Reflect.ownKeys(target), ...Object.keys(computeFunctions)];
    },

    // Object.keys only lists keys that have an enumerable descriptor
    getOwnPropertyDescriptor(target, property) {
      if (Object.hasOwn(computeFunctions, property)) {
        return { get: computeFunctions[property], enumerable: true, configurable: true };
      }
      return Reflect.getOwnPropertyDescriptor(target, property);
    }
  });
}

const startTime = Date.now();
const systemInfo = createVirtualObject({
  currentTime: () => new Date().toLocaleTimeString(),
  randomId: () => Math.random().toString(36).substr(2, 9),
  memoryUsage: () => process.memoryUsage?.() || 'Not available',
  uptime: () => Date.now() - startTime
});

console.log('Time:', systemInfo.currentTime);
console.log('Random ID:', systemInfo.randomId);
console.log('Available properties:', Object.keys(systemInfo));

Smart Arrays with Enhanced Features

Extend arrays with additional capabilities:

function createSmartArray(initialData = []) {
  const data = [...initialData];
  
  return new Proxy(data, {
    get(target, property, receiver) {
      // Handle negative indices
      if (typeof property === 'string' && /^-\d+$/.test(property)) {
        const index = parseInt(property);
        const realIndex = target.length + index;
        return realIndex >= 0 ? target[realIndex] : undefined;
      }
      
      // Add utility methods
      switch (property) {
        case 'first': return target[0];
        case 'last': return target[target.length - 1];
        case 'isEmpty': return target.length === 0;
        case 'unique': 
          return () => [...new Set(target)];
        case 'shuffle':
          return () => {
            const shuffled = [...target];
            for (let i = shuffled.length - 1; i > 0; i--) {
              const j = Math.floor(Math.random() * (i + 1));
              [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
            }
            return shuffled;
          };
        default:
          return Reflect.get(target, property, receiver);
      }
    },

    set(target, property, value, receiver) {
      // Handle negative indices for setting
      if (typeof property === 'string' && /^-\d+$/.test(property)) {
        const index = parseInt(property);
        const realIndex = target.length + index;
        if (realIndex >= 0) {
          target[realIndex] = value;
          return true;
        }
        return false;
      }
      
      return Reflect.set(target, property, value, receiver);
    }
  });
}

const smartArray = createSmartArray(['a', 'b', 'c', 'd', 'e']);
console.log('Last item:', smartArray[-1]); // 'e'
console.log('First item:', smartArray.first); // 'a'
console.log('Unique items:', smartArray.unique());

smartArray[-1] = 'modified';
console.log('Modified array:', smartArray);

Advanced Function Interception

Create sophisticated function wrappers:

function createInterceptedFunction(originalFunction, interceptors = {}) {
  const callHistory = [];
  
  return new Proxy(originalFunction, {
    apply(target, thisArg, argumentsList) {
      const callId = Math.random().toString(36).substr(2, 9);
      const startTime = performance.now();
      
      // Before interceptor
      if (interceptors.before) {
        const result = interceptors.before(argumentsList, thisArg, callId);
        if (result !== undefined) argumentsList = result;
      }

      let result, error;
      
      try {
        result = Reflect.apply(target, thisArg, argumentsList);
      } catch (e) {
        error = e;
        if (interceptors.error) {
          const errorResult = interceptors.error(e, argumentsList, thisArg, callId);
          if (errorResult !== undefined) {
            result = errorResult;
            error = null;
          }
        }
        
        if (error) {
          callHistory.push({
            callId,
            arguments: argumentsList,
            error: error.message,
            duration: performance.now() - startTime,
            timestamp: new Date()
          });
          throw error;
        }
      }

      // After interceptor
      if (interceptors.after) {
        const afterResult = interceptors.after(result, argumentsList, thisArg, callId);
        if (afterResult !== undefined) result = afterResult;
      }

      callHistory.push({
        callId,
        arguments: argumentsList,
        result: result,
        duration: performance.now() - startTime,
        timestamp: new Date()
      });

      return result;
    },
    
    get(target, property) {
      if (property === 'callHistory') return [...callHistory];
      if (property === 'clearHistory') return () => callHistory.length = 0;
      if (property === 'getStats') {
        return () => ({
          totalCalls: callHistory.length,
          averageDuration: callHistory.reduce((sum, call) => sum + call.duration, 0) / callHistory.length,
          errorRate: callHistory.filter(call => call.error).length / callHistory.length
        });
      }
      return Reflect.get(target, property);
    }
  });
}

// Example usage with comprehensive monitoring
function expensiveCalculation(operation, a, b) {
  switch (operation) {
    case 'add': return a + b;
    case 'multiply': return a * b;
    case 'divide':
      if (b === 0) throw new Error('Division by zero');
      return a / b;
    default: throw new Error('Unknown operation');
  }
}

const monitoredFunction = createInterceptedFunction(expensiveCalculation, {
  before: (args, thisArg, callId) => {
    console.log(`[${callId}] Starting operation:`, args[0]);
    return args;
  },
  
  after: (result, args, thisArg, callId) => {
    console.log(`[${callId}] Operation completed:`, result);
    return Math.round(result * 100) / 100; // Round to 2 decimals
  },
  
  error: (error, args, thisArg, callId) => {
    console.log(`[${callId}] Operation failed:`, error.message);
    return NaN; // Return NaN instead of throwing
  }
});

console.log(monitoredFunction('add', 5, 3)); // 8
console.log(monitoredFunction('divide', 10, 0)); // NaN
console.log('Function stats:', monitoredFunction.getStats());

Observable Objects for Data Binding

Create objects that automatically notify when changed:

function createObservable(target, callback) {
  const changeHistory = [];
  
  return new Proxy(target, {
    set(obj, property, value) {
      const oldValue = obj[property];
      const result = Reflect.set(obj, property, value);
      
      if (result && oldValue !== value) {
        const change = {
          type: 'set',
          property,
          value,
          oldValue,
          timestamp: new Date()
        };
        changeHistory.push(change);
        callback(change);
      }
      return result;
    },

    deleteProperty(obj, property) {
      const oldValue = obj[property];
      const hadProperty = property in obj;
      const result = Reflect.deleteProperty(obj, property);
      
      if (result && hadProperty) {
        const change = {
          type: 'delete',
          property,
          oldValue,
          timestamp: new Date()
        };
        changeHistory.push(change);
        callback(change);
      }
      return result;
    },
    
    get(obj, property) {
      if (property === Symbol.for('changeHistory')) {
        return [...changeHistory];
      }
      return Reflect.get(obj, property);
    }
  });
}

// Usage in a reactive system
const state = createObservable({ count: 0, name: 'App' }, (change) => {
  console.log('State changed:', change);
  updateUI(); // Trigger UI updates
});

state.count++; // Automatically triggers UI update
state.name = 'My App'; // Automatically triggers UI update

Dynamic API Client Creation

Build API clients that create methods on demand:

function createAPIClient(baseConfig = {}) {
  const cache = new Map();
  const requestHistory = [];
  
  return new Proxy({}, {
    get(target, property) {
      // Skip internal properties
      if (typeof property === 'string' && property.startsWith('_')) {
        return Reflect.get(target, property);
      }
      
      // Utility methods
      if (property === 'clearCache') return () => cache.clear();
      if (property === 'getHistory') return () => [...requestHistory];
      
      // Return API method
      return async function(config = {}) {
        const finalConfig = { ...baseConfig, ...config };
        const cacheKey = `${property}:${JSON.stringify(config)}`;
        const canCache = finalConfig.useCache && (config.method || 'GET') === 'GET';
        
        // Check cache
        if (canCache && cache.has(cacheKey)) {
          return cache.get(cacheKey);
        }
        
        const startTime = performance.now();
        
        try {
          // Simulate API call
          const response = await fetch(`${finalConfig.baseURL}/${property}`, {
            method: config.method || 'GET',
            headers: finalConfig.headers || {},
            body: config.body ? JSON.stringify(config.body) : undefined
          });
          
          if (!response.ok) {
            throw new Error(`Request failed with status ${response.status}`);
          }
          
          const data = await response.json();
          const duration = performance.now() - startTime;
          
          requestHistory.push({
            endpoint: property,
            config,
            duration,
            success: true,
            timestamp: new Date()
          });
          
          if (canCache) {
            cache.set(cacheKey, data);
          }
          
          return data;
        } catch (error) {
          requestHistory.push({
            endpoint: property,
            config,
            duration: performance.now() - startTime,
            success: false,
            error: error.message,
            timestamp: new Date()
          });
          throw error;
        }
      };
    }
  });
}

// Usage creates methods dynamically
const api = createAPIClient({ 
  baseURL: 'https://api.example.com',
  useCache: true,
  headers: { 'Authorization': 'Bearer token' }
});

// These methods are created on-the-fly
const users = await api.users();
const posts = await api.posts({ method: 'POST', body: { title: 'New Post' } });
console.log('API call history:', api.getHistory());

The Reflect API in Practice

Reflect provides a consistent way to perform meta-operations:

class MetaOperations {
  constructor(target) {
    this.target = target;
  }

  // Safer property access
  safeGet(property, defaultValue = undefined) {
    return Reflect.has(this.target, property) 
      ? Reflect.get(this.target, property)
      : defaultValue;
  }

  // Conditional property setting
  setIfValid(property, value, validator) {
    if (validator(value)) {
      return Reflect.set(this.target, property, value);
    }
    return false;
  }

  // Dynamic method invocation
  callMethod(methodName, ...args) {
    const method = Reflect.get(this.target, methodName);
    if (typeof method === 'function') {
      return Reflect.apply(method, this.target, args);
    }
    throw new Error(`Method ${methodName} not found`);
  }

  // Property enumeration with filtering
  getFilteredKeys(predicate) {
    return Reflect.ownKeys(this.target)
      .filter(key => predicate(key, this.target[key]));
  }

  // Safe object extension
  extend(source) {
    for (const key of Reflect.ownKeys(source)) {
      const descriptor = Reflect.getOwnPropertyDescriptor(source, key);
      if (descriptor) {
        Reflect.defineProperty(this.target, key, descriptor);
      }
    }
    return this.target;
  }
}

// Example usage
const obj = { name: 'Test', count: 5 };
const meta = new MetaOperations(obj);

console.log(meta.safeGet('name')); // 'Test'
console.log(meta.safeGet('missing', 'default')); // 'default'

meta.setIfValid('count', 10, val => val > 0); // true
meta.setIfValid('count', -5, val => val > 0); // false

const publicKeys = meta.getFilteredKeys(key => !key.startsWith('_'));
console.log('Public keys:', publicKeys);

Proxy Revocation for Security

Control access to sensitive objects:

function createSecureObject(data, accessToken) {
  let isRevoked = false;
  
  const { proxy, revoke } = Proxy.revocable(data, {
    get(target, property) {
      if (isRevoked) throw new Error('Access revoked');
      console.log(`Accessing ${property} with token ${accessToken}`);
      return Reflect.get(target, property);
    },
    
    set(target, property, value) {
      if (isRevoked) throw new Error('Access revoked');
      console.log(`Setting ${property} with token ${accessToken}`);
      return Reflect.set(target, property, value);
    }
  });

  // Return both proxy and a secure revoke function
  return {
    data: proxy,
    revoke: () => {
      isRevoked = true;
      revoke();
    },
    isRevoked: () => isRevoked
  };
}

const secure = createSecureObject({ secret: 'confidential' }, 'token123');
console.log(secure.data.secret); // Works

secure.revoke(); // Revoke access

try {
  console.log(secure.data.secret); // Throws error
} catch (error) {
  console.log('Access denied:', error.message);
}

Performance Considerations

Understanding when to use proxies:

// Proxies add overhead - measure impact
function performanceComparison() {
  const plainObject = { x: 1, y: 2, z: 3 };
  const proxiedObject = new Proxy(plainObject, {
    get(target, property) {
      return Reflect.get(target, property);
    }
  });

  const iterations = 1000000;

  // Test plain object access
  console.time('Plain object access');
  for (let i = 0; i < iterations; i++) {
    const value = plainObject.x;
  }
  console.timeEnd('Plain object access');

  // Test proxied object access
  console.time('Proxied object access');
  for (let i = 0; i < iterations; i++) {
    const value = proxiedObject.x;
  }
  console.timeEnd('Proxied object access');
}

// Only use proxies when the benefits outweigh the overhead
performanceComparison();

Key Points Summary

  • Intercept operations: Proxy traps let you customize fundamental object operations
  • Reflect methods: Provide consistent API for meta-operations
  • Practical applications: Validation, debugging, API creation, data binding
  • Performance cost: Consider overhead vs. benefits
  • Security features: Revocable proxies for access control

What Interviewers Want to Hear

They want to see that you understand when meta-programming is appropriate and can implement practical solutions like debugging tools, validation systems, dynamic API clients, or observable patterns. Mention performance considerations and when you’d choose Proxy over other solutions.


Testing Asynchronous Code

This question assesses your ability to write reliable tests for real-world async scenarios and understanding of testing fundamentals.

Why This Question Matters

Modern JavaScript applications are heavily asynchronous, from API calls to user interactions. Testing async code properly is crucial for application reliability, but it’s also where many bugs hide. This question reveals whether you understand test isolation, can handle complex async flows, and know how to write maintainable test suites that catch real problems.

Testing async functions often involves understanding how the event loop processes promises and callbacks.
Some async tests rely on a callback function, while others use async/await with better control of scope and timing.

In frontend environments, this connects back to testing updates to the DOM and verifying UI changes on a web page built with HTML and CSS.

When writing tests, explicitly cover the catch block to verify error paths, and profile critical paths to understand overall code execution time.

The Complete Answer Framework

Start with the fundamental challenge: Async code testing requires ensuring tests wait for operations to complete, handle both success and failure cases properly, and isolate the code under test from external dependencies.

Proper Async Test Structure

Modern testing frameworks support async/await patterns. The Jest examples below share this top-level setup and assume UserService and the other services under test are defined elsewhere:

// Shared setup for all test suites below
let userService, mockApiClient, mockCache, mockLogger;

beforeEach(() => {
  // Fresh mocks for each test
  mockApiClient = { get: jest.fn(), post: jest.fn(), put: jest.fn(), delete: jest.fn() };
  mockCache = { get: jest.fn(), set: jest.fn(), delete: jest.fn() };
  mockLogger = { info: jest.fn(), error: jest.fn() };
  
  userService = new UserService(mockApiClient, mockCache, mockLogger);
});

// Clean async test structure
describe('User Service', () => {
  it('should fetch user successfully and cache result', async () => {
    // Arrange
    const expectedUser = { id: 1, name: 'John', email: 'john@test.com' };
    mockCache.get.mockResolvedValue(null);
    mockApiClient.get.mockResolvedValue(expectedUser);

    // Act
    const result = await userService.getUser(1);

    // Assert
    expect(result).toEqual(expectedUser);
    expect(mockCache.set).toHaveBeenCalledWith('user:1', expectedUser, { ttl: 300 });
    expect(mockLogger.info).toHaveBeenCalledWith('User 1 fetched successfully');
  });
});

Comprehensive Error Scenario Testing

Test both success and failure paths:

describe('Error Handling', () => {
  afterEach(() => jest.useRealTimers());

  it('should handle network timeouts gracefully', async () => {
    const timeoutError = new Error('Request timeout');
    timeoutError.code = 'ETIMEDOUT';
    
    mockApiClient.get.mockRejectedValue(timeoutError);

    await expect(userService.getUser(1))
      .rejects.toMatchObject({
        message: 'Request timeout',
        code: 'ETIMEDOUT'
      });
    
    expect(mockLogger.error).toHaveBeenCalledWith(
      'Failed to fetch user 1: Request timeout'
    );
  });

  it('should handle rate limiting with retry-after', async () => {
    jest.useFakeTimers();
    const rateLimitError = new Error('Too Many Requests');
    rateLimitError.status = 429;
    rateLimitError.headers = { 'retry-after': '60' };
    
    mockApiClient.get
      .mockRejectedValueOnce(rateLimitError)
      .mockResolvedValueOnce({ id: 1, name: 'John' });

    const resultPromise = userService.getUser(1);
    await jest.advanceTimersByTimeAsync(60000); // Skip the 60-second wait
    const result = await resultPromise;
    
    // Should retry after the delay and return the user
    expect(result).toEqual({ id: 1, name: 'John' });
    expect(mockApiClient.get).toHaveBeenCalledTimes(2);
  });

  it('should handle partial failures in batch operations', async () => {
    const batchRequests = [
      { id: 1, action: 'update' },
      { id: 2, action: 'delete' },
      { id: 3, action: 'update' }
    ];

    mockApiClient.put.mockResolvedValueOnce({ success: true });
    mockApiClient.delete.mockRejectedValueOnce(new Error('Delete failed'));
    mockApiClient.put.mockResolvedValueOnce({ success: true });

    const results = await userService.processBatch(batchRequests);

    expect(results).toEqual([
      { success: true },
      { success: false, error: 'Delete failed' },
      { success: true }
    ]);
  });
});

Testing Complex Async Flows

Handle sophisticated async patterns:

describe('Complex Async Patterns', () => {
  // Fake timers also fake Date.now(), so durations are deterministic
  beforeEach(() => jest.useFakeTimers());
  afterEach(() => jest.useRealTimers());

  it('should handle retry logic with exponential backoff', async () => {
    const serviceError = new Error('Service unavailable');
    
    // First two calls fail, third succeeds
    mockApiClient.get
      .mockRejectedValueOnce(serviceError)
      .mockRejectedValueOnce(serviceError)
      .mockResolvedValue({ id: 1, name: 'John' });

    const startTime = Date.now();
    const resultPromise = userService.fetchWithRetry(1);
    await jest.runAllTimersAsync(); // Skip the backoff delays
    const result = await resultPromise;
    const endTime = Date.now();

    expect(result).toEqual({ id: 1, name: 'John' });
    expect(mockApiClient.get).toHaveBeenCalledTimes(3);
    expect(endTime - startTime).toBeGreaterThan(100); // Verify backoff delay
  });

  it('should handle concurrent requests efficiently', async () => {
    const users = [
      { id: 1, name: 'Alice' },
      { id: 2, name: 'Bob' },
      { id: 3, name: 'Carol' }
    ];

    // Mock with different delays
    mockApiClient.get
      .mockImplementationOnce(() => 
        new Promise(resolve => setTimeout(() => resolve(users[0]), 100)))
      .mockImplementationOnce(() => 
        new Promise(resolve => setTimeout(() => resolve(users[1]), 50)))
      .mockImplementationOnce(() => 
        new Promise(resolve => setTimeout(() => resolve(users[2]), 150)));

    const startTime = Date.now();
    const resultsPromise = Promise.all([
      userService.getUser(1),
      userService.getUser(2),
      userService.getUser(3)
    ]);
    await jest.runAllTimersAsync();
    const results = await resultsPromise;
    const endTime = Date.now();

    expect(results).toEqual(users);
    expect(endTime - startTime).toBeLessThan(200); // Should run concurrently
  });
});

Advanced Mocking Strategies

Service-Level Mocking

Mock entire service layers:

// Mock entire API service
class MockApiService {
  constructor() {
    this.responses = new Map();
    this.delays = new Map();
    this.callCounts = new Map();
  }

  mockEndpoint(endpoint, response, delay = 0) {
    this.responses.set(endpoint, response);
    this.delays.set(endpoint, delay);
    this.callCounts.set(endpoint, 0);
  }

  async request(endpoint, options = {}) {
    const count = this.callCounts.get(endpoint) || 0;
    this.callCounts.set(endpoint, count + 1);

    const delay = this.delays.get(endpoint) || 0;
    if (delay > 0) {
      await new Promise(resolve => setTimeout(resolve, delay));
    }

    const response = this.responses.get(endpoint);
    if (response instanceof Error) {
      throw response;
    }

    return response || { error: 'Not mocked' };
  }

  // Same get() interface as the apiClient mocks above
  get(url) {
    return this.request(url);
  }

  getCallCount(endpoint) {
    return this.callCounts.get(endpoint) || 0;
  }
}

describe('Service Integration Tests', () => {
  let mockApi;

  beforeEach(() => {
    mockApi = new MockApiService();
    userService = new UserService(mockApi, mockCache, mockLogger);
  });

  it('should handle API service integration', async () => {
    mockApi.mockEndpoint('/users/1', { id: 1, name: 'John' });
    mockApi.mockEndpoint('/users/1/profile', { bio: 'Developer' });

    const userData = await userService.getUserWithProfile(1);

    expect(userData).toMatchObject({
      user: { id: 1, name: 'John' },
      profile: { bio: 'Developer' }
    });
    
    expect(mockApi.getCallCount('/users/1')).toBe(1);
    expect(mockApi.getCallCount('/users/1/profile')).toBe(1);
  });
});

Time-Based Testing

Test time-dependent functionality:

describe('Time-Dependent Operations', () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });

  afterEach(() => {
    jest.useRealTimers();
  });

  it('should timeout requests after specified duration', async () => {
    const slowPromise = new Promise(resolve => 
      setTimeout(() => resolve('slow response'), 10000));
    mockApiClient.get.mockReturnValue(slowPromise);

    const fetchPromise = userService.getUser(1, { timeout: 5000 });
    
    const assertion = expect(fetchPromise).rejects.toThrow('Request timeout');

    // Fast-forward time to trigger timeout
    await jest.advanceTimersByTimeAsync(5000);
    
    await assertion;
  });

  it('should debounce rapid successive calls', async () => {
    const searchService = new DebouncedSearchService(mockApiClient);
    
    // Make rapid successive calls
    const promise1 = searchService.search('john');
    const promise2 = searchService.search('john');
    const promise3 = searchService.search('john');
    
    // Fast-forward past debounce delay
    await jest.advanceTimersByTimeAsync(500);
    
    await Promise.all([promise1, promise2, promise3]);
    
    // Should only make one API call due to debouncing
    expect(mockApiClient.get).toHaveBeenCalledTimes(1);
  });

  it('should implement circuit breaker pattern', async () => {
    const circuitBreakerService = new CircuitBreakerUserService(mockApiClient);
    
    // Simulate failures to trip circuit breaker
    mockApiClient.get.mockRejectedValue(new Error('Service down'));

    // Make enough failed requests to trip circuit breaker
    for (let i = 0; i < 5; i++) {
      await circuitBreakerService.getUser(1).catch(() => {});
    }

    // Next request should fail fast without calling the API
    await expect(circuitBreakerService.getUser(1))
      .rejects.toThrow('Circuit breaker is open');

    expect(mockApiClient.get).toHaveBeenCalledTimes(5);
  });
});

Testing Async Generators and Iterators

Handle modern async patterns:

describe('Async Generators', () => {
  async function* mockDataStream() {
    const data = [1, 2, 3, 4, 5];
    for (const item of data) {
      await new Promise(resolve => setTimeout(resolve, 10));
      yield item;
    }
  }

  it('should process async generator streams', async () => {
    const results = [];
    
    for await (const value of mockDataStream()) {
      results.push(value);
    }
    
    expect(results).toEqual([1, 2, 3, 4, 5]);
  });

  it('should handle async generator errors gracefully', async () => {
    async function* errorStream() {
      yield 1;
      yield 2;
      throw new Error('Stream error');
    }

    const results = [];
    
    try {
      for await (const value of errorStream()) {
        results.push(value);
      }
    } catch (error) {
      expect(error.message).toBe('Stream error');
    }
    
    expect(results).toEqual([1, 2]);
  });
});

Custom Test Utilities

Create reusable testing helpers:

// Custom test utilities
class AsyncTestHelpers {
  static async waitFor(condition, timeout = 1000) {
    const start = Date.now();
    while (Date.now() - start < timeout) {
      if (await condition()) return;
      await new Promise(resolve => setTimeout(resolve, 10));
    }
    throw new Error(`Condition not met within ${timeout}ms`);
  }

  static createMockTimer() {
    let currentTime = 0;
    return {
      advance: (ms) => { currentTime += ms; },
      now: () => currentTime,
      reset: () => { currentTime = 0; }
    };
  }

  static async expectEventually(assertion, timeout = 1000) {
    const start = Date.now();
    let lastError;
    
    while (Date.now() - start < timeout) {
      try {
        await assertion();
        return; // Success
      } catch (error) {
        lastError = error;
        await new Promise(resolve => setTimeout(resolve, 10));
      }
    }
    
    throw lastError || new Error('Assertion timed out');
  }
}

// Usage in tests
describe('Custom Test Utilities', () => {
  it('should wait for async conditions', async () => {
    let isReady = false;
    setTimeout(() => { isReady = true; }, 100);

    await AsyncTestHelpers.waitFor(() => isReady);
    expect(isReady).toBe(true);
  });

  it('should assert eventually', async () => {
    let counter = 0;
    const interval = setInterval(() => counter++, 10);

    await AsyncTestHelpers.expectEventually(() => {
      expect(counter).toBeGreaterThan(5);
    });

    clearInterval(interval);
  });
});

Performance and Load Testing

Test system behavior under load:

describe('Performance Testing', () => {
  it('should handle high concurrency without degradation', async () => {
    const concurrentRequests = 100;
    const requests = Array.from({ length: concurrentRequests }, (_, i) => 
      userService.getUser(i + 1)
    );

    mockApiClient.get.mockImplementation((url) => {
      const id = parseInt(url.split('/').pop());
      return Promise.resolve({ id, name: `User ${id}` });
    });

    const startTime = performance.now();
    const results = await Promise.all(requests);
    const endTime = performance.now();

    expect(results).toHaveLength(concurrentRequests);
    expect(endTime - startTime).toBeLessThan(1000);
    expect(mockApiClient.get).toHaveBeenCalledTimes(concurrentRequests);
  });

  it('should maintain memory efficiency with large datasets', async () => {
    const largeDataset = Array.from({ length: 10000 }, (_, i) => ({ id: i }));
    
    mockApiClient.get.mockResolvedValue(largeDataset);

    const startMemory = process.memoryUsage?.().heapUsed || 0;
    const result = await userService.processLargeDataset();
    const endMemory = process.memoryUsage?.().heapUsed || 0;

    expect(result).toBeDefined();
    // Memory usage shouldn't grow significantly
    expect(endMemory - startMemory).toBeLessThan(50 * 1024 * 1024); // 50MB
  });
});

Testing Best Practices Summary

Essential principles:

  1. Isolation: Each test should be independent and not affect others
  2. Mocking: Mock external dependencies to test your code, not external services
  3. Coverage: Test both success and failure scenarios, including edge cases
  4. Assertions: Verify both the result and the side effects
  5. Maintainability: Write tests that are easy to understand and modify

Common pitfalls to avoid:

  • Not waiting for async operations to complete
  • Testing implementation details instead of behavior
  • Forgetting to reset mocks between tests
  • Not handling Promise rejections properly
  • Relying on real timers for time-dependent tests
  • Not testing error scenarios thoroughly

Key Points Summary

  • Async/await patterns: Use modern syntax for cleaner tests
  • Comprehensive mocking: Isolate dependencies and test edge cases
  • Error scenario coverage: Test failures as thoroughly as success cases
  • Time management: Use fake timers for time-dependent functionality
  • Performance awareness: Test system behavior under load

What Interviewers Want to Hear

They want to see that you understand test isolation, proper mocking strategies, how to verify both successful and error scenarios, and can write maintainable test suites. Mention specific tools you’ve used (Jest, Mocha, Sinon), patterns you follow (AAA – Arrange, Act, Assert), and how you handle different types of async operations.

Key Takeaways for Your Next Interview

When answering these JavaScript interview questions, interviewers evaluate more than syntax knowledge.

Practical Understanding

Explain when and why to use specific features, not just how they work. Articulate the business value and technical benefits of choosing one approach over another.

Real-World Experience

Reference specific project scenarios where you’ve implemented these concepts. Explain the challenges you faced and how JavaScript features solved them.

Problem-Solving Approach

Show your ability to choose appropriate tools for different situations. Discuss trade-offs and demonstrate informed architectural thinking.

Code Quality Awareness

Address performance implications, maintainability concerns, and how your choices affect team productivity and codebase health.

Topic-Specific Preparation

  • Core questions: Answer scope, closures, this, the event loop, and promises in two or three sentences, then back each answer with a short example.

ES6 Modules vs CommonJS

Demonstrate understanding of tree-shaking benefits and bundle optimization. Clearly explain the live bindings versus copies distinction. Recommend systems based on project requirements and show knowledge of interoperability strategies for mixed environments.

Arrow Functions

Focus on this binding differences with concrete examples. Explain where regular functions remain superior and discuss performance implications in class-heavy applications. Show how arrows enable clean functional programming patterns.

Generators and Iterators

Emphasize memory efficiency with large datasets through lazy evaluation. Highlight async iteration patterns for modern APIs. Demonstrate state machine implementations and algorithmic problem-solving beyond simple iteration.

Proxy and Reflect

Balance advanced knowledge with practical judgment. Explain when meta-programming provides value versus simpler solutions. Show practical applications like debugging tools while discussing performance trade-offs and security implications.

Async Testing

Focus on test reliability in asynchronous environments. Demonstrate proper isolation and mocking strategies. Cover error scenarios comprehensively and show understanding of time-dependent testing and performance challenges.

Communication Strategy

The best answers combine technical accuracy with practical examples from your own experience. Practice explaining these concepts in terms of problems they solve rather than just features they provide.

Structure your answers like this:

  1. Define the concept clearly – Show you understand the fundamentals
  2. Explain the key differences/benefits – Demonstrate deeper knowledge
  3. Provide practical examples – Show real-world application
  4. Discuss trade-offs – Reveal mature engineering judgment
  5. Share experience – Connect to your actual project work

Final Interview Tips

Be honest about your knowledge: If you don’t know something, say so and explain how you would learn it. Curiosity and problem-solving ability often matter more than memorizing every JavaScript feature.

Ask clarifying questions: When given coding challenges, ask about requirements, constraints, and expected edge cases. This shows thoughtful engineering approach.

Consider the bigger picture: Connect technical concepts to business value, user experience, and team productivity when appropriate.

Stay current: Mention that you keep up with JavaScript evolution and can adapt to new features as they emerge.

Practice with real code: Don’t just memorize concepts – build small projects that use these features so you can speak from experience.

Remember, the goal isn’t to demonstrate encyclopedic knowledge but to show that you can write maintainable, reliable JavaScript code that works well in real-world applications. Focus on understanding the “why” behind each concept, and you’ll be well-prepared for even the most challenging JavaScript interviews.

If you want structured, hands-on practice before your interview, check out the Mimo JavaScript Course. It’s designed to help you build real projects, strengthen your coding confidence, and prepare you for technical questions in a practical way.

Henry Ameseder

AUTHOR

Henry Ameseder

Henry is the COO and a co-founder of Mimo. Since joining the team in 2016, he’s been on a mission to make coding accessible to everyone. Passionate about helping aspiring developers, Henry creates valuable content on programming, writes Python scripts, and in his free time, plays guitar.

Learn to code and land your dream job in tech

Start for free