JavaScript Interview Questions
Explain what a JavaScript expression does before you discuss how to optimize it. These questions separate language behavior from how the browser schedules work.
Try answering aloud before opening each explanation.
Try the JavaScript practice demoFoundations
How can a const object still change?
Const prevents reassignment of the variable binding, not mutation of the object. Object.freeze is a separate operation and is shallow unless nested objects are also frozen.
When does an arrow function get its this value?
It captures this from the surrounding scope. Calling it as an object property or using call does not give it a new receiver, unlike an ordinary unbound method.
What does a closure retain after its outer function returns?
It retains access to captured lexical bindings. It is not necessarily a snapshot of their values, so later changes to a shared binding can be visible.
What is the difference between ?? and || for defaults?
The nullish operator falls back only for null and undefined. The logical OR operator also falls back for falsy values such as zero, false and the empty string.
Why can two Date objects with the same timestamp fail strict equality?
Object equality by === compares identity. Compare the numeric timestamps when the intent is to compare instants.
Applied
Why can invoking a stored reference to a method lose its receiver?
An ordinary function's this depends on how it is called. Extracting obj.method and invoking it separately loses the object receiver; bind it or call it through a wrapper.
Does object spread make a deep copy?
No. It copies own enumerable properties into a new outer object, but nested object values still refer to the same objects. Copy the nested paths you intend to change.
What does a browser run first after the current task: a queued microtask or a timer callback?
The browser drains microtasks at the checkpoint before moving on to another task. A timer callback is a separate task, even with zero delay.
What happens if a callback passed to then throws?
The promise returned by that then call rejects with the thrown error. Return or await the chain so the consumer can observe the failure.
Does Array.forEach wait for an async callback to finish?
No. Its iteration is synchronous and it discards callback return values. Await a for-of loop for sequential work or an explicitly bounded pool for parallel work.
Production scenarios
How do you prevent a slow autocomplete response from replacing newer results?
Track the latest request or generation and apply only its result. Abort prior requests where possible, but retain the generation check because cancellation can race with a response.
How can an event listener cause a detached element to remain in memory?
A listener attached to a long-lived object can close over the element. Dispose of the listener when the component unmounts and inspect the heap's retaining paths to confirm the leak.
Why is sorting state in place risky in a reactive application?
Array.sort mutates the original array, so code sharing the reference sees unexpected changes. Return a new array when deriving a sorted view, using toSorted where supported.
How would you keep a browser responsive while processing a large local dataset?
Split work into tasks that yield to rendering, or move sustained computation to a worker. Consider serialization and memory costs, and measure long tasks rather than assuming smaller chunks are enough.
How do you parse an optional user-entered count without turning a blank into zero?
Distinguish blank input before numeric conversion, then check the resulting number is finite and an integer within range. A coercion result alone cannot tell you what the user intended.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- Docker interview questions
- MongoDB interview questions
- PostgreSQL interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions