Skip to content
DevFlash

By technology

JavaScript interview questions: examples and explained answers

Questions about language behavior, with small examples whose results you can predict and explain.

  • 5 min read
  • 6 sections
  • Updated November 2, 2026

This guide is under editorial review.

In 1 minute

  • Explain rules with examples and reason again when a condition changes.
  • Distinguish values, references, shallow copies and domain comparison criteria.
  • For asynchronous ordering and events, explain the mechanism before predicting interface behavior.

JavaScript interview questions become easier when you can predict a result and explain each step. A short definition can open an answer, but an example shows whether you understand the rule. Rather than memorizing trick questions, practice scope, object identity and asynchronous execution with small snippets you can trace on paper.

This guide focuses on language behavior and browser interaction. Static modeling belongs in the TypeScript questions, while component rendering belongs in the React questions. During practice, answer before running the code. Then execute the snippet, compare your prediction and record which rule you misunderstood.

Answering JavaScript interview questions

  1. Structure the answer

    Structure your answer around a rule, an example and a limitation.

  2. Show an example

    To explain a closure, say what is retained, show a function reading an outer variable and describe when that variable changes.

  3. Reassess the change

    If the interviewer changes a line, reason through it again rather than defending the previous output. Adaptation shows more understanding than a memorized definition.

  • State whether the code runs in a browser, module or another environment.
  • Identify primitive values, references and mutation points.
  • Separate immediate execution from scheduled work.
  • When uncertain, propose a smaller example to check.

Avoid performance claims without context. Saying one loop is always better does not address input size, readability or observed cost. Establish correctness first, then discuss complexity and measurements that could justify optimization.

Scope, closures and variable capture

A closure lets a function access the lexical environment where it was created. That does not mean copying every value at creation time. Here, the returned function reads prefix, whose value changed before the call. The output is updated:item. Predicting that the function remembers only the original value would be incorrect.

function createLabel(): (value: string) => string {
let prefix = "initial";
const label = (value: string) => `${prefix}:${value}`;
prefix = "updated";
return label;
}
console.log(createLabel()("item"));

Also consider reference lifetime. An active listener can keep accessing captured data. That does not make every closure a memory leak; the issue depends on what remains reachable and for how long. In loops, let creates per-iteration bindings, while var is function-scoped. Explain that distinction before analyzing callbacks that all print the same number.

Equality, coercion and missing values

Strict equality does not coerce operands to make them comparable: the number 0 and string "0" differ. That does not mean === solves every domain comparison. Two objects with identical fields remain distinct references. Comparing entities may require an identifier; comparing content requires deciding which fields matter.

Distinguish missing values from falsy values. The ?? operator falls back only for null and undefined; || also replaces zero and empty strings. If zero is a valid quantity, quantity || 1 changes the business rule. Also note that NaN is not strictly equal to itself; Number.isNaN checks for it without converting strings to numbers.

Objects, references and array transformations

When passing an object to a function, the passed value refers to the same object. Reassigning the parameter does not replace the caller's variable, but changing a property mutates the shared object. Spread creates a shallow copy: nested objects remain shared. Below, the original ends with enabled set to false because both structures reference the same settings object.

const original = { settings: { enabled: true } };
const copy = { ...original };
copy.settings.enabled = false;
console.log(original.settings.enabled); // false
const independent = {
...original,
settings: { ...original.settings, enabled: true },
};
console.log(independent.settings.enabled); // true

For arrays, explain the desired transformation before choosing map, filter or reduce. A new array does not guarantee new nested objects. A mapping callback that modifies each received item still changes shared data. That matters in stateful interfaces and warrants checking the input after the operation.

Promises, async/await and execution order

This example runs in a browser. Synchronous code prints start and end first. The resolved promise callback runs as a microtask before the timer callback in this scenario. A zero timer delay does not mean immediate execution or an exact deadline. The current task must finish, and the browser manages other work too.

console.log("start");
setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("promise"));
console.log("end");

An async function returns a promise. At await, it suspends its continuation without blocking the entire thread. That does not turn expensive computation into parallel work. Starting independent requests before awaiting them can avoid serial waiting; dependent operations still need ordering. Explain rejection and failure behavior before proposing Promise.all.

An event loop diagram with a call stack column, a browser APIs box, two queues and a circular arrow linking them.
Distinguish synchronous execution, microtasks and scheduled tasks to justify the example’s output order.

Events and DOM interaction

Do

  • Explain the difference between target, where the event originated, and currentTarget, the element whose listener is executing. With delegation, narrow the target before accessing element-specific properties.
  • preventDefault cancels a cancelable default action; stopPropagation affects propagation. Choose according to the required behavior.

Avoid

  • Treating preventDefault and stopPropagation as interchangeable.
  • Applying both automatically to hide a flow you have not understood.

Connect the answer to the interface: native buttons offer keyboard behavior that a div does not gain from a click listener alone. Review accessibility in interviews and verify interactions with front-end automated tests. A definition becomes useful when you explain its effect on the person using the screen.

Frequently asked questions

What JavaScript should I study for interviews?

Prioritize scope, closures, value types, references, arrays, promises and events. Practice an example and a variation for each topic. Explain outputs rather than repeating context-free definitions.

How do I explain closures with an example?

Return a function that reads an outer variable. Change that variable before calling the function and explain why it observes the binding rather than necessarily retaining an initial-value snapshot.

How does strict equality differ from coercion?

Strict equality compares without type conversion. Abstract equality applies conversion rules in some cases. Even with strict equality, objects are compared by identity rather than field contents.

How do I explain promise and timer execution order?

Start with synchronous code and identify when callbacks are scheduled. In this guide's example, the promise microtask precedes the timer. State the environment and avoid generalizing to every asynchronous combination.

Practice with real tests

Solve front-end exercises in the browser, with the same tests a reviewer would run.

See the exercises