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.
Showing 40 of 40 questions
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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,β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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β¦
- 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 `β¦
- 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β¦
- 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β¦
- 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,β¦