JavaScript & React quiz and interview questions
Front-end interviews love tricky JavaScript output questions — type coercion, closures in loops and the order of the event loop — plus React bugs like infinite re-renders and missing effect dependencies. Work through them below, junior to senior.
JavaScript & React: key concepts
- == vs ===
- === compares value and type with no conversion. == converts types first, which leads to surprises like "0" == 0 being true.
- var, let and const
- var is function-scoped and shared across loop iterations. let and const are block-scoped, so each loop iteration gets its own binding.
- The event loop
- Synchronous code runs first, then microtasks (promise callbacks), then macrotasks (setTimeout).
- References
- Objects and arrays are assigned by reference, so const b = a points to the same array.
- React state
- Never call setState during render — it triggers another render, forever. Update state in event handlers or effects.
- useEffect dependencies
- No dependency array runs the effect after every render. [] runs it once. List every value the effect reads.
All 14 JavaScript & React questions with answers
Try each one first, then open the answer. Making a class test? Use the test maker.
What does this print?
console.log(typeof null);- A. "null"
- B. "object"
- C. "undefined"
- D. Error
Show answer
B. "object" — A long-standing JavaScript quirk: typeof null is "object". Check for null with === null.
What does this print?
console.log(1 + "2", 1 - "2");- A. 3 -1
- B. "12" -1
- C. 12 -1
- D. 12 NaN
Show answer
C. 12 -1 — + with a string concatenates: "12". - only works on numbers, so "2" is converted: 1 - 2 = -1. console.log prints 12 -1.
What does this print?
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); }- A. 0 1 2
- B. 3 3 3
- C. 2 2 2
- D. undefined ×3
Show answer
B. 3 3 3 — var is shared by all iterations, and the callbacks run after the loop has finished, when i is 3.
What does this print?
for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); }- A. 0 1 2
- B. 3 3 3
- C. 2 2 2
- D. undefined ×3
Show answer
A. 0 1 2 — let creates a new binding for each iteration, so each callback remembers its own i.
In what order are the letters printed?
console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D");- A. A B C D
- B. A D C B
- C. A D B C
- D. A C D B
Show answer
B. A D C B — Sync code first (A, D), then microtasks (the promise: C), then macrotasks (setTimeout: B).
What is the difference between == and ===?
- A. None
- B. === compares value and type without conversion; == converts types first
- C. == is faster
- D. === only works on strings
Show answer
B. === compares value and type without conversion; == converts types first — Use === by default. With ==, "" == 0 and "0" == 0 are both true.
What happens when this component renders?
function Counter() { const [count, setCount] = useState(0); setCount(count + 1); return <p>{count}</p>; }- A. Shows 1
- B. Infinite re-render loop ("Too many re-renders")
- C. Shows 0
- D. Compile error
Show answer
B. Infinite re-render loop ("Too many re-renders") — Calling setCount during render schedules another render, which calls setCount again, forever. Update state in an event handler or effect.
Why does React need a key on list items?
- A. For styling
- B. To identify items between renders so it updates the right elements
- C. For SEO
- D. To sort the list
Show answer
B. To identify items between renders so it updates the right elements — Stable keys (like an id) let React match items when the list changes. Using the array index breaks state when items are reordered or removed.
What's wrong with this effect?
useEffect(() => { fetch("/api/user") .then(r => r.json()) .then(setUser); });- A. fetch can't be used in effects
- B. No dependency array, so it re-fetches after every render — and setUser causes another render
- C. setUser must be awaited
- D. Nothing
Show answer
B. No dependency array, so it re-fetches after every render — and setUser causes another render — Without a dependency array the effect runs after every render, and setUser triggers a render, so it loops. Add [] to run it once.
What does this print?
console.log([1, 2, 3].map(x => x * 2));- A. [1, 2, 3]
- B. [2, 4, 6]
- C. 12
- D. undefined
Show answer
B. [2, 4, 6] — map returns a new array with the function applied to each element.
What is a main benefit of React Server Components?
- A. They run only in the browser
- B. They render on the server and send no component JavaScript to the browser
- C. They replace CSS
- D. They need no data
Show answer
B. They render on the server and send no component JavaScript to the browser — Server Components can read data directly and ship HTML without their JS, keeping bundles small. Interactive parts stay Client Components.
What does this print?
const a = [1, 2, 3]; const b = a; b.push(4); console.log(a.length);- A. 3
- B. 4
- C. Error: a is const
- D. undefined
Show answer
B. 4 — b points to the same array as a. const stops reassigning the variable, not changing the array's contents.
Why debounce a search box?
- A. To encrypt input
- B. To wait until the user stops typing before sending a request, cutting needless API calls
- C. To make typing faster
- D. To validate email
Show answer
B. To wait until the user stops typing before sending a request, cutting needless API calls — Debouncing delays the call until there's a pause (e.g. 300 ms), so typing "laptop" sends one request instead of six.
What does this print?
console.log(0.1 + 0.2 === 0.3);- A. true
- B. false
- C. NaN
- D. Error
Show answer
B. false — Floating-point numbers can't represent 0.1 and 0.2 exactly; the sum is 0.30000000000000004. Compare with a small tolerance instead.
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.