<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: web dev</title>
    <description>The latest articles on DEV Community by web dev (@web_dev_f8cc17f0bd281ad18).</description>
    <link>https://dev.to/web_dev_f8cc17f0bd281ad18</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4185413%2F4b8bd9fe-2ca4-43b2-823d-23b7ebd064fb.png</url>
      <title>DEV Community: web dev</title>
      <link>https://dev.to/web_dev_f8cc17f0bd281ad18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/web_dev_f8cc17f0bd281ad18"/>
    <language>en</language>
    <item>
      <title>Predict the Output: 4 JavaScript and React Quiz Questions That Test Understanding, Not Memory</title>
      <dc:creator>web dev</dc:creator>
      <pubDate>Thu, 15 Oct 2026 18:40:08 +0000</pubDate>
      <link>https://dev.to/web_dev_f8cc17f0bd281ad18/predict-the-output-4-javascript-and-react-quiz-questions-that-test-understanding-not-memory-4l1j</link>
      <guid>https://dev.to/web_dev_f8cc17f0bd281ad18/predict-the-output-4-javascript-and-react-quiz-questions-that-test-understanding-not-memory-4l1j</guid>
      <description>&lt;p&gt;You can write JavaScript every day and still be surprised by the order a few lines log in. Syntax is easy to remember, but behaviour is harder: when a callback runs, which variable a closure captures, and what React does with three &lt;code&gt;setState&lt;/code&gt; calls in a row.&lt;/p&gt;

&lt;p&gt;The quickest way to find out which of those you really understand is to &lt;strong&gt;predict the output before running the code&lt;/strong&gt;. If your prediction and your reasoning are both right, you understand the concept. If only the prediction is right, you have a gap that hasn't caused a bug yet.&lt;/p&gt;

&lt;p&gt;This post has four questions of that kind, two in JavaScript and two in React. Each comes with the answer, a short explanation and the concept it tests. A companion repository has twelve more, with links to the official documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;What the question tests&lt;/th&gt;
&lt;th&gt;Practical relevance&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Event loop&lt;/td&gt;
&lt;td&gt;Ordering of sync code, microtasks and timers&lt;/td&gt;
&lt;td&gt;Explains why a &lt;code&gt;.then()&lt;/code&gt; runs before a &lt;code&gt;setTimeout(…, 0)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Closures and &lt;code&gt;var&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Which binding a callback captures&lt;/td&gt;
&lt;td&gt;A classic source of "every handler sees the last value" bugs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;React state updates&lt;/td&gt;
&lt;td&gt;State as a snapshot during one render&lt;/td&gt;
&lt;td&gt;Why repeated &lt;code&gt;setCount(count + 1)&lt;/code&gt; calls don't add up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Stale closures in effects&lt;/td&gt;
&lt;td&gt;What an effect with &lt;code&gt;[]&lt;/code&gt; dependencies can see&lt;/td&gt;
&lt;td&gt;Timers and subscriptions that "freeze" at an old value&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Why quiz yourself at all?
&lt;/h2&gt;

&lt;p&gt;A quiz question can't tell you how good a developer you are. What it can do well is narrower and more useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Find gaps cheaply.&lt;/strong&gt; A wrong prediction in a five-line snippet costs nothing. The same misunderstanding in production code costs a debugging session.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate recall from reasoning.&lt;/strong&gt; Writing down &lt;em&gt;why&lt;/em&gt; you picked an answer shows you whether you knew it or recognised it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rehearse explaining.&lt;/strong&gt; Technical interviews often ask you to talk through what code does. Explaining a short snippet out loud is good practice for that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Treat each question below as a test of one concept. Write your answer down before you open the explanation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question 1: Sync, microtasks and timers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concept tested:&lt;/strong&gt; the event loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nf"&gt;queueMicrotask&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;D&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;E&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What gets logged, in order?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A. &lt;code&gt;A B C D E&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;B. &lt;code&gt;A E B C D&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;C. &lt;code&gt;A E C D B&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;D. &lt;code&gt;A C D E B&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Answer
  &lt;br&gt;
&lt;strong&gt;C. &lt;code&gt;A E C D B&lt;/code&gt;&lt;/strong&gt;

&lt;p&gt;The synchronous code runs first, logging &lt;code&gt;A&lt;/code&gt; and then &lt;code&gt;E&lt;/code&gt;. While it runs, three callbacks are queued. The &lt;code&gt;setTimeout&lt;/code&gt; callback goes into the &lt;strong&gt;task&lt;/strong&gt; (macrotask) queue. The &lt;code&gt;.then()&lt;/code&gt; callback and the &lt;code&gt;queueMicrotask&lt;/code&gt; callback go into the &lt;strong&gt;microtask&lt;/strong&gt; queue, in that order.&lt;/p&gt;

&lt;p&gt;When the script finishes, the runtime drains the entire microtask queue before picking up the next task. That logs &lt;code&gt;C&lt;/code&gt;, then &lt;code&gt;D&lt;/code&gt;. Only then does the timer callback run and log &lt;code&gt;B&lt;/code&gt;. A delay of &lt;code&gt;0&lt;/code&gt; means "as soon as a task can run", not "immediately".&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; if you've ever seen state read "too early" in a &lt;code&gt;setTimeout&lt;/code&gt; or "too late" after an &lt;code&gt;await&lt;/code&gt;, this ordering is usually why. MDN's &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide" rel="noopener noreferrer"&gt;microtask guide&lt;/a&gt; covers it in depth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question 2: Closures in a &lt;code&gt;var&lt;/code&gt; loop
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concept tested:&lt;/strong&gt; closures and function-scoped variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A. &lt;code&gt;0 1 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;B. &lt;code&gt;3 3 3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;C. &lt;code&gt;2 2 2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;D. &lt;code&gt;undefined undefined undefined&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Answer
  &lt;br&gt;
&lt;strong&gt;B. &lt;code&gt;3 3 3&lt;/code&gt;&lt;/strong&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; is function-scoped, so there is a single &lt;code&gt;i&lt;/code&gt; shared across all three iterations. Each arrow function closes over that same variable, not over its value at the time. By the time the timers fire, the loop has finished and &lt;code&gt;i&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Change &lt;code&gt;var&lt;/code&gt; to &lt;code&gt;let&lt;/code&gt; and the output becomes &lt;code&gt;0 1 2&lt;/code&gt;. A &lt;code&gt;let&lt;/code&gt; declaration in a &lt;code&gt;for&lt;/code&gt; loop header creates a new binding for each iteration, so each callback captures its own &lt;code&gt;i&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; the same mistake shows up in event handlers attached in loops and in async callbacks inside &lt;code&gt;for&lt;/code&gt; loops. See &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#creating_closures_in_loops_a_common_mistake" rel="noopener noreferrer"&gt;MDN on closures&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question 3: Three updates in one click
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concept tested:&lt;/strong&gt; React state as a snapshot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After &lt;strong&gt;one&lt;/strong&gt; click, what does the button show?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A. &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;B. &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;C. &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;D. React warns about too many updates and shows &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Answer
  &lt;br&gt;
&lt;strong&gt;B. &lt;code&gt;1&lt;/code&gt;&lt;/strong&gt;

&lt;p&gt;During a render, &lt;code&gt;count&lt;/code&gt; is a constant: it's &lt;code&gt;0&lt;/code&gt; for the entire &lt;code&gt;handleClick&lt;/code&gt; call. Each line therefore asks React to set the state to &lt;code&gt;0 + 1&lt;/code&gt;. React batches the three requests and re-renders once with &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To build on the previous value, pass an updater function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// now shows 3 after one click&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React queues updater functions and applies each one to the result of the previous one.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; this explains a whole class of "my counter or cart total is off by some amount" bugs. The React docs cover it in &lt;a href="https://react.dev/learn/queueing-a-series-of-state-updates" rel="noopener noreferrer"&gt;Queueing a series of state updates&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Question 4: An interval that stops counting
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concept tested:&lt;/strong&gt; stale closures in effects&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Ticker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does the component show after five seconds?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A. &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;B. &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;C. &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;D. It re-renders in an infinite loop&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;
  Answer
  &lt;br&gt;
&lt;strong&gt;B. &lt;code&gt;1&lt;/code&gt;&lt;/strong&gt;

&lt;p&gt;With an empty dependency array, the effect runs once after mount, and its interval callback closes over the &lt;code&gt;count&lt;/code&gt; from that first render, which is &lt;code&gt;0&lt;/code&gt;. Every second it calls &lt;code&gt;setCount(0 + 1)&lt;/code&gt;. The first call changes the state to &lt;code&gt;1&lt;/code&gt;. After that, each call sets the same value, so the display stays at &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are two common fixes. You can use the updater form, &lt;code&gt;setCount((c) =&amp;gt; c + 1)&lt;/code&gt;, which doesn't need &lt;code&gt;count&lt;/code&gt; at all. Or you can list &lt;code&gt;count&lt;/code&gt; as a dependency, which restarts the interval on every change. For a timer, the updater form is simpler.&lt;/p&gt;

&lt;p&gt;(In development with &lt;code&gt;&amp;lt;StrictMode&amp;gt;&lt;/code&gt;, React runs the effect, cleans it up and runs it again. The cleanup clears the first interval, so the result is the same.)&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; the same pattern affects WebSocket handlers, event listeners and polling loops set up inside effects. The React docs explain it under &lt;a href="https://react.dev/learn/removing-effect-dependencies" rel="noopener noreferrer"&gt;Removing effect dependencies&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways for developers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Explain, don't just answer.&lt;/strong&gt; If you can't say &lt;em&gt;why&lt;/em&gt; &lt;code&gt;C&lt;/code&gt; logs before &lt;code&gt;B&lt;/code&gt;, a right answer is a guess. Mark it and come back to it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microtasks run before the next task.&lt;/strong&gt; Promise callbacks, &lt;code&gt;queueMicrotask&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; continuations all run before any &lt;code&gt;setTimeout&lt;/code&gt; callback, however short the delay.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Closures capture variables, not values.&lt;/strong&gt; Whether that variable is shared (&lt;code&gt;var&lt;/code&gt;) or new for each iteration (&lt;code&gt;let&lt;/code&gt;) changes the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In React, &lt;code&gt;state&lt;/code&gt; is fixed for the duration of a render.&lt;/strong&gt; When the next value depends on the previous one, use an updater function, especially inside timers and effects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sort your misses by concept.&lt;/strong&gt; Two wrong answers about the event loop say more than one wrong answer each on unrelated topics. Study the concept, then try fresh questions on it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Twelve more questions on GitHub
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://github.com/web4593/javascript-react-quiz-questions" rel="noopener noreferrer"&gt;companion repository of JavaScript and React quiz questions&lt;/a&gt; has twelve more predict-the-output questions, all different from the ones above. They cover &lt;code&gt;typeof&lt;/code&gt;, the temporal dead zone, where &lt;code&gt;async&lt;/code&gt; functions pause, &lt;code&gt;map(parseInt)&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt; on empty arrays, coercion, shallow copies, &lt;code&gt;this&lt;/code&gt; in callbacks, &lt;code&gt;Promise.allSettled&lt;/code&gt;, and three React behaviours. Each answer links to MDN or react.dev, and the README ends with a guide to grouping your misses by concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want a bigger question bank?
&lt;/h2&gt;

&lt;p&gt;If you'd like to keep going after these sixteen questions, &lt;a href="https://journeyuncommon.com/" rel="noopener noreferrer"&gt;JourneyUncommon&lt;/a&gt; is an interview-practice site with a multiple-choice question bank organised by language and topic. It covers JavaScript, TypeScript, React, Python, SQL, Go and more. You can browse the &lt;a href="https://journeyuncommon.com/javascript-mcq" rel="noopener noreferrer"&gt;JavaScript questions&lt;/a&gt; or the &lt;a href="https://journeyuncommon.com/quiz/category/react/hooks" rel="noopener noreferrer"&gt;React hooks questions&lt;/a&gt; without signing in, and the code is shown on the page.&lt;/p&gt;

&lt;p&gt;A free account lets you answer questions and see your score. It also tracks your accuracy by topic and brings questions you got wrong back for review later. There's also a &lt;a href="https://journeyuncommon.com/daily" rel="noopener noreferrer"&gt;daily question&lt;/a&gt; and a &lt;a href="https://journeyuncommon.com/arena" rel="noopener noreferrer"&gt;bug arena&lt;/a&gt;, where you diagnose a program that gives the wrong output, with up to three hints. A paid Pro plan raises the daily limits on the AI features, but you don't need it to practise the multiple-choice questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;How many did you get right &lt;em&gt;with&lt;/em&gt; the right reasoning? Question 4 is the one most likely to cause a real bug, because the code looks correct and works for exactly one second.&lt;/p&gt;

&lt;p&gt;If you have a favourite "predict the output" snippet that catches people out, share it in the comments. If it fits, it could become the next question in the repo.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
