JavaScript interview questions

18 questions from the JavaScript Fundamentals: From Syntax to Async course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Basics

1. What does `typeof null` return?

  1. "null"
  2. "undefined"
  3. "object"
  4. "function"
Show answer

Answer: "object"

It's a long-standing JS bug kept for backward compatibility.

Lesson: Data Types

Module 3: Control Flow

2. Which loop is best for iterating an object's own property keys?

  1. for...of
  2. while
  3. do...while
  4. for...in
Show answer

Answer: for...in

`for...in` is built specifically for enumerating an object's keys.

Lesson: Loops

Module 5: Arrays

3. Which method returns a new array shorter than or equal to the original?

  1. filter
  2. map
  3. forEach
  4. reduce
Show answer

Answer: filter

`filter` only keeps items that pass the test, so the result can never be longer than the input.

Lesson: Iteration: map, filter, reduce, forEach

4. Why does [10, 9, 1].sort() give [1, 10, 9]?

  1. It compares items as strings by default
  2. Sorting is random
  3. Numbers cannot be sorted
  4. It sorts by length
Show answer

Answer: It compares items as strings by default

Pass (a, b) => a - b for numeric order.

Lesson: Searching & Sorting Arrays

Module 6: Objects

5. What does `const [a, , b] = [1, 2, 3]` set b to?

  1. 2
  2. 3
  3. undefined
  4. 1
Show answer

Answer: 3

The empty slot skips index 1, so b picks up index 2, which holds 3.

Lesson: Destructuring Arrays & Objects

Module 8: Scope & Closures

6. In the counter example, why does calling counter() a second time return 2, not 1?

  1. Because count resets every call
  2. Because JS caches the last return value
  3. Because the closure keeps count alive between calls
  4. Because count is a global variable
Show answer

Answer: Because the closure keeps count alive between calls

The inner function closes over `count`, so it persists across separate calls to `counter()`.

Lesson: Closures

Module 9: The DOM

7. What does event.preventDefault() do?

  1. Stops the event from bubbling
  2. Removes the event listener
  3. Reloads the page
  4. Stops the browser's default action for that event
Show answer

Answer: Stops the browser's default action for that event

For example, it stops a form's default submit-and-reload behavior — bubbling is a separate concern handled by stopPropagation().

Lesson: Event Listeners & Bubbling

Module 12: Asynchronous JS

8. What does .catch() handle in a Promise chain?

  1. Any rejection from earlier in the chain
  2. Only network errors
  3. Nothing — it's just for logging
  4. Only synchronous errors
Show answer

Answer: Any rejection from earlier in the chain

A rejection at any earlier .then() skips the remaining .then() calls and jumps straight to the nearest .catch().

Lesson: Promises

Module 16: Misc

9. Why doesn't setTimeout(fn, 0) run fn immediately?

  1. Because 0ms actually means never
  2. Because it queues fn to run after the current call stack finishes
  3. Because setTimeout is fully synchronous
  4. Because setTimeout is deprecated
Show answer

Answer: Because it queues fn to run after the current call stack finishes

Its callback still waits for the call stack to clear, even with a 0ms delay.

Lesson: JSON, Regex & the Event Loop

10. What does `typeof null` return?

  1. "null"
  2. "undefined"
  3. "object"
  4. "number"
Show answer

Answer: "object"

A long-standing quirk: null reports as object.

Lesson: JSON, Regex & the Event Loop

11. Which declaration is block-scoped and cannot be reassigned?

  1. var
  2. let
  3. function
  4. const
Show answer

Answer: const

const is block-scoped and fixes the binding.

Lesson: JSON, Regex & the Event Loop

12. What does `[1,2,3].map(n => n * 2)` return?

  1. [2, 4, 6]
  2. 6
  3. undefined
  4. [1, 2, 3]
Show answer

Answer: [2, 4, 6]

map returns a new array.

Lesson: JSON, Regex & the Event Loop

13. Which operator falls back only on null or undefined?

  1. ||
  2. ??
  3. &&
  4. ==
Show answer

Answer: ??

|| also falls back on 0 and empty strings.

Lesson: JSON, Regex & the Event Loop

14. What is a closure?

  1. A way to close a browser tab
  2. A loop
  3. A function that remembers variables from where it was created
  4. A module
Show answer

Answer: A function that remembers variables from where it was created

Closures keep access to their creation scope.

Lesson: JSON, Regex & the Event Loop

15. What does event.preventDefault() do?

  1. Stops bubbling
  2. Removes the listener
  3. Reloads the page
  4. Stops the browser's default action
Show answer

Answer: Stops the browser's default action

For example it stops a form from submitting.

Lesson: JSON, Regex & the Event Loop

16. What does `await` do inside an async function?

  1. Pauses that function until the promise settles
  2. Blocks the whole page
  3. Cancels the promise
  4. Runs code twice
Show answer

Answer: Pauses that function until the promise settles

Other code keeps running while it waits.

Lesson: JSON, Regex & the Event Loop

17. Why does setTimeout(fn, 0) not run immediately?

  1. 0 means never
  2. fn is queued until the call stack is clear
  3. setTimeout is synchronous
  4. It needs a promise
Show answer

Answer: fn is queued until the call stack is clear

The event loop runs queued callbacks after the current code.

Lesson: JSON, Regex & the Event Loop

18. What does user.profile?.name return when profile is null?

  1. undefined
  2. A TypeError
  3. null
  4. ""
Show answer

Answer: undefined

Optional chaining short-circuits to undefined.

Lesson: Modern Syntax: Optional Chaining & More

← Back to the course