JavaScript interview questions

// 40 QUESTIONS Β· UPDATED MAY 2026

JavaScript interview questions scoped for QA and SDET roles. Closures, scope, async/await, prototypes, ES modules, event loop, and patterns you actually use writing Cypress, Playwright, and Node-based test code.

Level

Showing 40 of 40 questions

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

    var is function-scoped and hoisted (initialised to undefined before its declaration line runs); let and const are block-scoped and throw…

  2. Explain closures in JavaScript with a practical example.Mid

    A closure is a function bundled with live references to the variables in its surrounding lexical scope. Even after the outer function ret…

  3. What is the event loop and how does it differ from threads?Senior

    JavaScript is single-threaded; the event loop processes one task at a time from a queue. Unlike OS threads, there is no parallel executio…

  4. What is the difference between `==` and `===` in JavaScript?Junior

    `===` (strict equality) checks value AND type without coercion. `==` coerces types first, so `0 == false` is true but `0 === false` is fa…

  5. What is hoisting in JavaScript, and how does it affect `var`, `let`, and `const`?Junior

    Hoisting moves `var` declarations and function declarations to the top of their scope before execution. `var` is initialized as `undefine…

  6. What is the difference between `null` and `undefined` in JavaScript?Junior

    `undefined` means a variable exists but has no assigned value β€” the engine's default. `null` is an intentional assignment meaning 'no val…

  7. What is a callback function, and why are they common in JavaScript?Junior

    A callback is a function passed as an argument and invoked after an operation completes. JavaScript's single-threaded, non-blocking model…

  8. How do arrow functions differ from regular functions in JavaScript?Junior

    Arrow functions inherit `this` from the enclosing scope (lexical `this`) instead of binding it dynamically. They cannot be constructors,…

  9. What does `JSON.stringify()` do, and what are its gotchas?Junior

    `JSON.stringify()` converts a JavaScript value to a JSON string. It silently drops `undefined`, functions, and Symbol-keyed properties. C…

  10. What are template literals, and what are tagged templates?Junior

    Template literals use backtick delimiters and support `${expr}` interpolation and multi-line strings without escape sequences. Tagged tem…

  11. How does destructuring assignment work in JavaScript?Junior

    Destructuring extracts values from arrays (by position) or object properties (by name) into variables. It supports defaults, renaming, ne…

  12. What is the difference between `map()`, `filter()`, `reduce()`, and `forEach()`?Junior

    `map()` transforms each element into a new same-length array. `filter()` returns a new array with only matching elements. `reduce()` accu…

  13. What values can `typeof` return, and what are its known quirks?Junior

    `typeof` returns one of: 'undefined', 'boolean', 'number', 'bigint', 'string', 'symbol', 'function', or 'object'. Notable quirks: `typeof…

  14. What is the difference between the spread operator and rest parameters?Junior

    Both use `...` syntax but in opposite directions. Spread expands an iterable or object into individual elements at a call site or literal…

  15. How does JavaScript's event loop enable asynchronous programming on a single thread?Mid

    JavaScript has one call stack. Async operations are handed off to the runtime. When they complete, callbacks are queued. The event loop p…

  16. What is a Promise, and how do you handle fulfillment, rejection, and cleanup?Mid

    A Promise represents an eventual value in one of three immutable states: pending, fulfilled, or rejected. Use `.then()` for fulfilled val…

  17. When should you use async/await over `.then()` chains?Mid

    `async`/`await` makes sequential async steps read like synchronous code, improving readability and debugging (better stack traces). `.the…

  18. What is the difference between `Promise.all()`, `Promise.allSettled()`, `Promise.race()`, and `Promise.any()`?Mid

    `Promise.all` resolves when all resolve, fails fast on first rejection. `Promise.allSettled` always resolves with outcome objects for eve…

  19. How does the `this` keyword work across different JavaScript contexts?Mid

    `this` is determined at call time, not definition time (except arrow functions). In a method call `this` is the object; in a constructor…

  20. How does prototypal inheritance work in JavaScript, and how does `class` relate to it?Mid

    Every object has an internal `[[Prototype]]` reference. Property lookups traverse the chain until found or null. `class` syntax is syntac…

  21. What is the difference between `call()`, `apply()`, and `bind()`?Mid

    All three explicitly set `this`. `call(ctx, arg1, arg2)` invokes immediately with individual args. `apply(ctx, [args])` invokes immediate…

  22. What is the difference between ES Modules and CommonJS, and why does it matter for test tooling?Mid

    CommonJS (`require`/`module.exports`) is synchronous and dynamic. ES Modules (`import`/`export`) are static, asynchronous, and tree-shake…

  23. What is the `Symbol` type in JavaScript, and when is it useful?Mid

    Symbol creates a guaranteed-unique primitive β€” every `Symbol()` call produces a value unequal to any other. Symbols are non-enumerable in…

  24. What is the difference between a shallow copy and a deep copy in JavaScript?Mid

    A shallow copy duplicates the top-level structure but nested objects are still shared references. A deep copy recursively clones every le…

  25. How do race conditions occur in async JavaScript, and how do you prevent them in tests?Mid

    Race conditions occur when multiple async operations compete and their interleaved execution order is non-deterministic. In tests, unreso…

  26. What is the difference between microtasks and macrotasks in the JavaScript event loop?Mid

    Microtasks (Promise callbacks, queueMicrotask) run immediately after the current task and drain completely before the next macrotask. Mac…

  27. How should you handle errors in async JavaScript code?Mid

    Use `try/catch` around `await` expressions for async/await code. For Promise chains, attach `.catch()` at the end. In Node.js, handle `un…

  28. What is a JavaScript `Proxy`, and what can it intercept?Mid

    A `Proxy` wraps an object and intercepts fundamental operations via handler traps: `get`, `set`, `has`, `deleteProperty`, `apply`, and mo…

  29. How do you correctly test Promise-based code in Jest?Mid

    Return or await the Promise in the test β€” otherwise Jest finishes before the assertion runs. Use `expect(promise).resolves.toBe()` for fu…

  30. How do optional chaining (`?.`) and nullish coalescing (`??`) work?Mid

    Optional chaining `?.` short-circuits to `undefined` if the left side is `null` or `undefined`, preventing TypeError on deep property acc…

  31. What are generator functions in JavaScript, and when are they useful?Mid

    Generator functions (`function*`) return an iterator that can pause at `yield` expressions and resume on `.next()`. They produce values l…

  32. How do you identify and fix memory leaks in a long-running Node.js test process?Senior

    Memory leaks in Node.js tests typically come from event listeners not removed, closures holding large objects, globals accumulating state…

  33. What is the difference between `for...of`, `for...in`, and `forEach()`, and when should each be used?Senior

    `for...of` iterates over iterable values (arrays, strings, Maps, Sets, generators) using the iterator protocol. `for...in` iterates over…

  34. What is the temporal dead zone (TDZ), and how does it affect `let`, `const`, and `class`?Senior

    The TDZ is the period between when a `let`/`const`/`class` binding is created (block entry) and when it is initialized (the declaration l…

  35. How do you implement debounce and throttle, and when do you use each?Senior

    Debounce delays execution until a period of inactivity β€” the function runs after the last call if no new call arrives within the delay. T…

  36. What is the `Reflect` API, and how does it complement `Proxy`?Senior

    `Reflect` provides static methods mirroring Proxy traps, allowing clean forwarding to the target's default behaviour inside trap handlers…

  37. What are the practical differences between ES6 `class` and prototype-based patterns, and when does it matter?Senior

    `class` is syntactic sugar over prototypes β€” methods go on the prototype, instances share them. Practical differences: `class` enforces `…

  38. What does `Object.freeze()` do, and how does it compare to TypeScript's `readonly`?Senior

    `Object.freeze()` is a runtime enforcement β€” frozen object properties cannot be added, deleted, or modified. It is shallow: nested object…

  39. When would you keep a test automation project in plain JavaScript rather than migrating to TypeScript?Lead

    Keep JavaScript when the team's TypeScript proficiency is low and the migration cost outweighs the benefit, when the project is short-liv…

  40. How do you lead a JavaScript-to-TypeScript migration for a large automation suite?Lead

    Migrate incrementally: enable `allowJs` + `checkJs` first to catch errors without converting. Prioritise shared utilities (Page Objects,…