JavaScript interview questions with model answers

25 questions written alongside the free JavaScript course, each with the answer an interviewer is listening for. All of them are below, in course order.

What JavaScript interviews test

JavaScript interviews lean on the runtime model more than the syntax: how closures capture scope, what this refers to, how the event loop orders callbacks, promises and async/await, prototypes, and the rules behind == coercion. Expect to predict a short snippet's output and explain why.

  1. 1. Is JavaScript interpreted or compiled?

    Both, in practice. You ship source and the engine parses it to bytecode and starts running immediately, so it behaves as interpreted. But V8 then just-in-time compiles hot functions to machine code, so long-running code ends up genuinely compiled.

    From the lesson What JavaScript Is and Where It Runs

  2. 2. What is Automatic Semicolon Insertion and when does it bite?

    The parser inserts semicolons where a statement cannot continue. It bites after return with the value on the next line, which silently returns undefined, and before a line starting with an opening bracket, which continues the previous statement instead.

    From the lesson Statements, Semicolons and Automatic Insertion

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

    var is function-scoped and hoisted to undefined. let and const are block-scoped and sit in a temporal dead zone until their declaration, so reading them early throws instead of silently giving undefined. const additionally prevents reassignment of the binding, though the value may still be mutable.

    From the lesson let, const and var

  4. 4. Why does typeof null return "object", and how do you check for null?

    It is a bug from the first implementation, where values were tagged by their low bits and null shared the object tag. It cannot be fixed without breaking existing sites. The correct check is value === null.

    From the lesson The Type System: Primitives and Objects

  5. 5. Why is 0.1 + 0.2 not 0.3, and how do you compare floats?

    Numbers are IEEE 754 doubles and 0.1 has no exact binary representation, so a tiny error appears. Compare the absolute difference against a small epsilon, and for money use integer minor units instead of floats.

    From the lesson Arithmetic, Numbers and Their Limits

  6. 6. What is the difference between == and ===, and when is == acceptable?

    === compares value and type with no conversion; == converts operands first following the abstract equality algorithm. Use === everywhere. The one widely accepted exception is value == null, which tests for null or undefined together.

    From the lesson Coercion, == vs === and Truthiness

  7. 7. What does "hello".toUpperCase() do to the original string?

    Nothing. Strings are immutable, so the method returns a new string and the original is unchanged. You have to assign the result.

    From the lesson String Methods and Immutability

  8. 8. What is a tagged template literal used for?

    It lets a function receive the literal string parts and the interpolated values as separate arguments, so it can process the values before assembling the result. That is how libraries escape HTML or parameterise SQL safely.

    From the lesson Template Literals

  9. 9. What happens if you forget break in a switch?

    Execution falls through into the following cases and runs their bodies too, until it hits a break or the end. Deliberate fall-through is used to group cases onto one outcome; accidental fall-through is a common bug.

    From the lesson Conditionals and switch

  10. 10. Why should you not use for...in on an array?

    It iterates enumerable keys rather than values, gives them as strings, skips holes, and includes inherited enumerable properties from the prototype chain. Use for...of for values or a classic for loop when you need a numeric index.

    From the lesson Loops: for, for...of, for...in and while

  11. 11. Which array methods mutate the original?

    push, pop, shift, unshift, splice, sort, reverse and fill. The ones that return a new array are slice, concat, map, filter, reduce, flat and the newer toSorted, toReversed and with. sort and reverse are the ones people forget, because they look like transformations.

    From the lesson Array Basics and Which Methods Mutate

  12. 12. What is the difference between map and forEach?

    map returns a new array of the callback’s return values; forEach returns undefined and exists for side effects. If you are calling map and discarding the result, you meant forEach — and if your map callback has braces with no return, every element becomes undefined.

    From the lesson map, filter, reduce and Friends

  13. 13. What does the spread operator copy?

    One level. { ...obj } creates a new object whose own enumerable properties are copied, but nested objects are still the same references. For a genuine deep copy use structuredClone.

    From the lesson Objects, Destructuring and Spread

  14. 14. What is the difference between a function declaration and an arrow function?

    Declarations are hoisted and have their own this, arguments and prototype, so they can be used as constructors and as methods. Arrows are not hoisted, take this from the enclosing scope, have no arguments object, and cannot be called with new.

    From the lesson Declarations, Expressions and Arrow Functions

  15. 15. What is a closure and where have you used one?

    A function together with the scope it was created in, which keeps those variables alive after the outer function returns. In practice: private state in a factory, configuration captured in a returned function, and every callback that refers to a variable from its surrounding scope.

    From the lesson Closures

  16. 16. How is this determined in JavaScript?

    By the call site, in this order: new binding, then explicit binding with call, apply or bind, then the object before the dot in a method call, then the default — undefined in strict mode. Arrow functions are the exception: they have no this of their own and take it lexically from where they were defined.

    From the lesson The this Keyword

  17. 17. How does inheritance work in JavaScript?

    Through the prototype chain. Every object has a link to a prototype object, and a property lookup walks that chain until it finds the property or reaches null. class and extends are syntax over that: methods go on the prototype, and extends sets one prototype’s link to another.

    From the lesson Classes, Prototypes and Inheritance

  18. 18. What order do console.log, setTimeout(…, 0) and Promise.resolve().then run in?

    Synchronous logs first, because the call stack runs to completion. Then the entire microtask queue drains, so the promise callback runs. Only then does the event loop take one macrotask, so the setTimeout callback runs last — even though it was scheduled first.

    From the lesson The Event Loop, Callbacks and Promises

  19. 19. How do you run several async operations in parallel?

    Start them all before awaiting: map to an array of promises and await Promise.all. Awaiting inside a loop serialises them, so three 100ms calls take 300ms instead of 100ms. Use allSettled when one failure should not abort the rest.

    From the lesson async / await and Error Handling

  20. 20. Why should you avoid a bare catch that handles everything?

    Because it catches your own mistakes too. A typo produces a ReferenceError that gets reported as whatever the catch block assumes went wrong, so the real bug is hidden. Check the error type and re-throw anything you did not expect.

    From the lesson Errors: throw, try/catch and Custom Error Types

  21. 21. What is lost when you round-trip an object through JSON?

    undefined values, functions and symbols are dropped; Dates become strings; NaN and Infinity become null; Maps, Sets and class instances lose their type and methods; BigInt throws; and circular references throw. That is why JSON.parse(JSON.stringify(x)) is a poor deep copy.

    From the lesson JSON: stringify, parse and Their Limits

  22. 22. When would you use a Map instead of a plain object?

    When keys are not strings — objects, numbers, anything — when they are dynamic and added or removed often, when I need a reliable size and strict insertion order, or when I want no inherited keys. I would use an object for fixed known fields and for anything that has to become JSON, since a Map serialises as {}.

    From the lesson Map, Set and the Weak Collections

  23. 23. What is a generator and when would you use one?

    A function that can pause and resume, declared with function* and yielding values on demand. It returns an object that is both iterable and an iterator. I would use one for lazy sequences — infinite streams, paging through large data, or tree traversal with yield*, where building the whole array first would be wasteful or impossible.

    From the lesson Iterators and Generators

  24. 24. What is the difference between debounce and throttle?

    Debounce delays the call until the events stop for a given period, so only the last one fires — right for a search-as-you-type box. Throttle lets the call through at most once per interval regardless of how many events arrive — right for scroll and resize handlers where you want regular updates.

    From the lesson Functional Patterns: Recursion, Currying, Memoisation and Throttling

  25. 25. What is the difference between a named and a default export?

    A file can have many named exports, imported by their exact names in braces, and at most one default export, imported without braces under any name. Named exports keep one canonical name across the codebase, which makes searching and refactoring reliable, so most teams standardise on them.

    From the lesson Modules: import, export and Scope

Learn it, then drill it

The JavaScript course teaches every one of these in context, with runnable examples. For drilling, the practice bank has 2,605 multiple-choice JavaScript questions with explanations.

Other subjects