{"id":"l-js-2-1","courseId":"javascript-fundamentals","lang":"en","kind":"lesson","summary":"Make your program choose between paths with if, else if and else, combine conditions with &amp;&amp;, || and !, pick values with the ternary operator, and predict which values count as truthy or falsy.","html":"<p>Pocket can add up what you spent. Now it should tell you how you are doing: fine, getting close to your budget, or over it. That means running different code depending on the data, which is what conditions are for.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">if, else if, else</h2>\n<p>An <code dir=\"ltr\">if</code> statement runs a block of code only when its condition is true:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"0\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> budgetCents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> spentCents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 1750</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (spentCents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budgetCents) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Over budget\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (spentCents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">>=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budgetCents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0.8</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Close to your budget\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"On track\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span></code></pre><div class=\"run-output\" data-run-output=\"0\" hidden></div></div>\n<p>The engine evaluates the first condition, <code dir=\"ltr\">1750 &gt; 2000</code>, gets <code dir=\"ltr\">false</code>, and moves on. The second, <code dir=\"ltr\">1750 &gt;= 1600</code>, is <code dir=\"ltr\">true</code>, so that block runs, and <strong>the rest of the chain is skipped</strong>. The <code dir=\"ltr\">else</code> block runs only when every condition above it was false. At most one block in a chain ever runs.</p>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 640 260\" role=\"img\" aria-labelledby=\"l-js-2-1-t1\"><defs><marker id=\"arrow-l-js-2-1-1\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path d=\"M0 0 L10 5 L0 10 z\" class=\"d-arrowhead\"/></marker></defs>\n  <title id=\"l-js-2-1-t1\">Flow of the budget check: first test spent greater than budget; if yes print Over budget. If no, test spent at least 80 percent of budget; if yes print Close. If no, print On track.</title>\n  <rect class=\"d-box-accent\" x=\"20\" y=\"30\" width=\"200\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"120\" y=\"60\" text-anchor=\"middle\">spent &gt; budget?</text>\n  <rect class=\"d-box-accent\" x=\"20\" y=\"120\" width=\"200\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"120\" y=\"150\" text-anchor=\"middle\">spent ≥ 80%?</text>\n  <rect class=\"d-box-success\" x=\"20\" y=\"205\" width=\"200\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-label\" x=\"120\" y=\"232\" text-anchor=\"middle\">\"On track\"</text>\n  <rect class=\"d-box-warn\" x=\"400\" y=\"30\" width=\"200\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-label\" x=\"500\" y=\"60\" text-anchor=\"middle\">\"Over budget\"</text>\n  <rect class=\"d-box-warn\" x=\"400\" y=\"120\" width=\"200\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-label\" x=\"500\" y=\"150\" text-anchor=\"middle\">\"Close\"</text>\n  <path class=\"d-arrow\" d=\"M220 55 L396 55\" marker-end=\"url(#arrow-l-js-2-1-1)\"/>\n  <text class=\"d-label-muted\" x=\"308\" y=\"46\" text-anchor=\"middle\">true: stop</text>\n  <path class=\"d-arrow\" d=\"M220 145 L396 145\" marker-end=\"url(#arrow-l-js-2-1-1)\"/>\n  <text class=\"d-label-muted\" x=\"308\" y=\"136\" text-anchor=\"middle\">true: stop</text>\n  <path class=\"d-arrow\" d=\"M120 80 L120 116\" marker-end=\"url(#arrow-l-js-2-1-1)\"/>\n  <text class=\"d-label-muted\" x=\"160\" y=\"103\">false</text>\n  <path class=\"d-arrow\" d=\"M120 170 L120 201\" marker-end=\"url(#arrow-l-js-2-1-1)\"/>\n  <text class=\"d-label-muted\" x=\"160\" y=\"192\">false</text>\n</svg><figcaption>An if chain stops at the first true condition</figcaption></figure>\n<p>Because the first true condition wins, <strong>order matters</strong>. Put the narrowest case first. If you checked “at least 80%” before “over budget”, an over-budget month would also be at least 80%, and you would never see the “Over budget” message. The quiz below has exactly that bug.</p>\n<p>The curly braces mark a <strong>block</strong>, a group of statements that run together. For a one-line body the braces are technically optional, but always write them: adding a second line later without braces is a classic bug where only the first line is conditional.</p>\n<h2 id=\"sec-2\" tabindex=\"-1\">Combining conditions</h2>\n<p>Three <strong>logical operators</strong> let you build bigger conditions:</p>\n<ul>\n<li><code dir=\"ltr\">a &amp;&amp; b</code> (and) is true only when both are true.</li>\n<li><code dir=\"ltr\">a || b</code> (or) is true when at least one is true.</li>\n<li><code dir=\"ltr\">!a</code> (not) flips true to false and back.</li>\n</ul>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"1\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"1\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amountCents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 12500</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> category</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"travel\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> isWeekend</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> false</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (amountCents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 10000</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> &#x26;&#x26;</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> category </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">!==</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"rent\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Big purchase: ask yourself twice\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (category </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">===</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"food\"</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> ||</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> category </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">===</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"travel\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Flexible spending\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">!</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">isWeekend) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Weekday\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span></code></pre><div class=\"run-output\" data-run-output=\"1\" hidden></div></div>\n<p><code dir=\"ltr\">&amp;&amp;</code> is evaluated before <code dir=\"ltr\">||</code>, just as <code dir=\"ltr\">*</code> is before <code dir=\"ltr\">+</code>. When you mix them, add parentheses so nobody, including future you, has to remember that.</p>\n<h2 id=\"sec-3\" tabindex=\"-1\">One value, many fixed options: switch</h2>\n<p>When you compare a single value against a list of exact options, such as a category, a chain of <code dir=\"ltr\">else if (category === …)</code> gets repetitive. The <code dir=\"ltr\">switch</code> statement is built for that case:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"2\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"2\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> category</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"travel\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> monthlyLimit;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">switch</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (category) {</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  case</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"food\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    monthlyLimit </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 40000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">    break</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  case</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"travel\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  case</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"fun\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    monthlyLimit </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 15000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">    break</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  default</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">:</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    monthlyLimit </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 10000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(monthlyLimit);</span></span></code></pre><div class=\"run-output\" data-run-output=\"2\" hidden></div></div>\n<p><code dir=\"ltr\">switch</code> compares with <code dir=\"ltr\">===</code>, jumps to the first matching <code dir=\"ltr\">case</code>, and runs from there <strong>until it reaches a <code dir=\"ltr\">break</code></strong>. Forget a <code dir=\"ltr\">break</code> and execution “falls through” into the next case, which is a frequent bug. Sometimes it is deliberate, as with <code dir=\"ltr\">&quot;travel&quot;</code> and <code dir=\"ltr\">&quot;fun&quot;</code> sharing one limit above. <code dir=\"ltr\">default</code> runs when nothing matches, like a final <code dir=\"ltr\">else</code>. For two or three conditions, or anything that is not a simple equality check, stay with <code dir=\"ltr\">if</code>.</p>\n<h2 id=\"sec-4\" tabindex=\"-1\">The ternary: choosing a value</h2>\n<p>Often you do not want to <em>do</em> different things, you want to <em>pick</em> between two values. The <strong>conditional operator</strong>, usually called the ternary, does that in one expression:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"3\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"3\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> spent</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2100</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> budget</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> status</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spent </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budget </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">?</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"Over budget\"</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> :</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"On track\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(status);</span></span></code></pre><div class=\"run-output\" data-run-output=\"3\" hidden></div></div>\n<p>Read it as “if <code dir=\"ltr\">spent &gt; budget</code>, then <code dir=\"ltr\">&quot;Over budget&quot;</code>, otherwise <code dir=\"ltr\">&quot;On track&quot;</code>”. Because the ternary produces a value, you can store it in a <code dir=\"ltr\">const</code> or drop it into a template literal. Use it for two outcomes. For three or more, an <code dir=\"ltr\">if</code> chain reads better than nested ternaries.</p>\n<h2 id=\"sec-5\" tabindex=\"-1\">Truthy and falsy</h2>\n<p>A condition does not have to be a boolean. JavaScript accepts any value and converts it: values that count as true are <strong>truthy</strong>, values that count as false are <strong>falsy</strong>. The falsy list is short enough to memorise:</p>\n<p><code dir=\"ltr\">false</code>, <code dir=\"ltr\">0</code>, <code dir=\"ltr\">-0</code>, <code dir=\"ltr\">0n</code>, <code dir=\"ltr\">&quot;&quot;</code> (empty string), <code dir=\"ltr\">null</code>, <code dir=\"ltr\">undefined</code>, <code dir=\"ltr\">NaN</code>.</p>\n<p>Everything else is truthy, including some that surprise people: <code dir=\"ltr\">&quot;0&quot;</code>, <code dir=\"ltr\">&quot;false&quot;</code>, <code dir=\"ltr\">&quot; &quot;</code> (a space), and empty lists and objects.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"4\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"4\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> note</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (note) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Has a note\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"No note\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">Boolean</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"0\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">), </span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">Boolean</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">), </span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">Boolean</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\" \"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">));</span></span></code></pre><div class=\"run-output\" data-run-output=\"4\" hidden></div></div>\n<p>Truthiness makes checks like <code dir=\"ltr\">if (note)</code> short and readable for strings, where “empty” and “missing” both mean “nothing to show”. It is risky for numbers, where 0 is often a real, valid value.</p>\n<aside class=\"callout callout-mistake\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3\"/><path d=\"M12 9v4\"/><path d=\"M12 17h.01\"/></svg><span><span class=\"callout-kind\">Common mistake</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Testing an amount with if (amount)</span></p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-index=\"5\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amountCents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// a free coffee voucher, recorded on purpose</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (amountCents) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Recorded\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Please enter an amount\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">); </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// runs, wrongly</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span></code></pre></div>\n<p>0 is falsy, so a legitimate zero looks like “nothing entered”. Say what you mean: <code dir=\"ltr\">if (typeof amountCents === &quot;number&quot; &amp;&amp; amountCents &gt;= 0)</code>.</p>\n</aside>\n<p><code dir=\"ltr\">&amp;&amp;</code> and <code dir=\"ltr\">||</code> also work with non-boolean values: they return one of their operands. <code dir=\"ltr\">name || &quot;Anonymous&quot;</code> gives <code dir=\"ltr\">name</code> if it is truthy and <code dir=\"ltr\">&quot;Anonymous&quot;</code> otherwise, which is a common way to supply defaults. It has the same zero problem, which is why JavaScript added a better operator for defaults, <code dir=\"ltr\">??</code>, coming up in section 3.</p>\n<aside class=\"callout callout-tip\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5\"/><path d=\"M9 18h6\"/><path d=\"M10 22h4\"/></svg><span><span class=\"callout-kind\">Tip</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Single = inside an if</span></p>\n<p><code dir=\"ltr\">if (category = &quot;food&quot;)</code> assigns instead of comparing, so the condition is always the truthy string <code dir=\"ltr\">&quot;food&quot;</code>. Comparisons use <code dir=\"ltr\">===</code>. Linters such as ESLint flag this; when your editor underlines it, read the warning.</p>\n</aside>\n<p>Conditions let Pocket react to one value. Next you will make it handle many values, one after another, with loops.</p>\n","toc":[{"id":"sec-1","text":"if, else if, else","level":2},{"id":"sec-2","text":"Combining conditions","level":2},{"id":"sec-3","text":"One value, many fixed options: switch","level":2},{"id":"sec-4","text":"The ternary: choosing a value","level":2},{"id":"sec-5","text":"Truthy and falsy","level":2}],"takeaways":["An <code dir=\"ltr\">if</code> chain checks conditions top to bottom and runs only the first block whose condition is true, so put the most specific case first.","<code dir=\"ltr\">&amp;&amp;</code> needs both sides true, <code dir=\"ltr\">||</code> needs at least one, and <code dir=\"ltr\">!</code> flips a boolean.","[object Object]","Only eight values are falsy (<code dir=\"ltr\">false</code>, <code dir=\"ltr\">0</code>, <code dir=\"ltr\">-0</code>, <code dir=\"ltr\">0n</code>, <code dir=\"ltr\">&quot;&quot;</code>, <code dir=\"ltr\">null</code>, <code dir=\"ltr\">undefined</code>, <code dir=\"ltr\">NaN</code>); everything else, including <code dir=\"ltr\">&quot;0&quot;</code> and <code dir=\"ltr\">[]</code>, is truthy."],"further":[{"title":"if...else (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else"},{"title":"Truthy (MDN glossary)","url":"https://developer.mozilla.org/en-US/docs/Glossary/Truthy"},{"title":"Conditional (ternary) operator (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator"}],"quiz":[{"q":"<p>With <code dir=\"ltr\">spent = 2100</code> and <code dir=\"ltr\">budget = 2000</code>, what does this log?</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (spent </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budget </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0.8</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"close\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (spent </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budget) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"over\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">} </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">else</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"ok\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span></code></pre></div>\n","options":[{"text":"<code dir=\"ltr\">over</code>","why":"The chain stops at the first true condition. 2100 is more than 1600, so the first block runs and the <code dir=\"ltr\">else if</code> is never checked."},{"text":"<code dir=\"ltr\">close</code>","why":"Correct, and it is a bug. The broader condition comes first and swallows the over-budget case. Check <code dir=\"ltr\">spent &gt; budget</code> first."},{"text":"<code dir=\"ltr\">close</code> and then <code dir=\"ltr\">over</code>","why":"An <code dir=\"ltr\">if</code>/<code dir=\"ltr\">else if</code> chain runs at most one block. Only separate <code dir=\"ltr\">if</code> statements could both run."},{"text":"<code dir=\"ltr\">ok</code>","why":"<code dir=\"ltr\">else</code> runs only when every condition above it is false, and the first one is true here."}],"answer":1},{"q":"<p>Which of these values is truthy?</p>","options":[{"text":"<code dir=\"ltr\">&quot;&quot;</code> (an empty string)","why":"The empty string is one of the eight falsy values."},{"text":"<code dir=\"ltr\">0</code>","why":"Zero is falsy, which is exactly why <code dir=\"ltr\">if (amount)</code> misbehaves for a real amount of 0."},{"text":"<code dir=\"ltr\">&quot;0&quot;</code> (a string containing zero)","why":"Correct. Any non-empty string is truthy, even <code dir=\"ltr\">&quot;0&quot;</code> and <code dir=\"ltr\">&quot;false&quot;</code>."},{"text":"<code dir=\"ltr\">NaN</code>","why":"<code dir=\"ltr\">NaN</code> is falsy, along with <code dir=\"ltr\">false</code>, <code dir=\"ltr\">0</code>, <code dir=\"ltr\">-0</code>, <code dir=\"ltr\">0n</code>, <code dir=\"ltr\">&quot;&quot;</code>, <code dir=\"ltr\">null</code> and <code dir=\"ltr\">undefined</code>."}],"answer":2},{"q":"<p>A user can set a daily limit, and <code dir=\"ltr\">0</code> is a valid choice meaning “no spending today”. What does <code dir=\"ltr\">const limit = userLimit || 5000;</code> do when <code dir=\"ltr\">userLimit</code> is <code dir=\"ltr\">0</code>?</p>","options":[{"text":"It keeps 0, because 0 is a number.","why":"<code dir=\"ltr\">||</code> does not check types; it checks truthiness, and 0 is falsy."},{"text":"It throws, because <code dir=\"ltr\">||</code> only works with booleans.","why":"<code dir=\"ltr\">||</code> works with any values and returns one of them, not necessarily a boolean."},{"text":"It sets <code dir=\"ltr\">limit</code> to 5000, silently ignoring the user’s choice.","why":"Correct. <code dir=\"ltr\">||</code> falls back whenever the left side is falsy, and 0 is falsy. The <code dir=\"ltr\">??</code> operator, covered in section 3, falls back only for null and undefined."}],"answer":2},{"q":"<p>Which line best uses the ternary operator?</p>","options":[{"text":"<code dir=\"ltr\">const label = isOver ? &quot;Over budget&quot; : &quot;On track&quot;;</code>","why":"Correct. The ternary picks one of two values and the result is stored. That is exactly what it is for."},{"text":"<code dir=\"ltr\">isOver ? sendAlert() : logQuietly();</code>","why":"This works, but it uses a value-picking operator to choose between actions. An <code dir=\"ltr\">if</code>/<code dir=\"ltr\">else</code> states that intent more clearly."},{"text":"<code dir=\"ltr\">const label = a ? b ? &quot;x&quot; : &quot;y&quot; : c ? &quot;z&quot; : &quot;w&quot;;</code>","why":"Nested ternaries are legal but hard to read. Use an <code dir=\"ltr\">if</code>/<code dir=\"ltr\">else if</code> chain once there are more than two outcomes."}],"answer":0}],"exercise":{"kind":"web","mode":"js","starter":{"js":"// The function wrapper is written for you (functions get their own lesson soon).\n// spentCents and budgetCents arrive as numbers. Use if / else if / else\n// and `return` the status string.\nfunction budgetStatus(spentCents, budgetCents) {\n  return \"ok\";\n}\n\n// Use a ternary to pick the label for one value\nfunction amountLabel(amountCents) {\n  return \"\";\n}\n\nconsole.log(budgetStatus(1750, 2000));\nconsole.log(amountLabel(12500));\n"},"solution":{"js":"function budgetStatus(spentCents, budgetCents) {\n  if (spentCents > budgetCents) {\n    return \"over\";\n  } else if (spentCents >= budgetCents * 0.8) {\n    return \"close\";\n  } else {\n    return \"ok\";\n  }\n}\n\nfunction amountLabel(amountCents) {\n  return amountCents >= 10000 ? \"big\" : \"small\";\n}\n\nconsole.log(budgetStatus(1750, 2000));\nconsole.log(amountLabel(12500));\n"},"checks":[{"id":"over","test":"return budgetStatus(2001, 2000) === 'over' && budgetStatus(5000, 2000) === 'over';\n"},{"id":"close","test":"return budgetStatus(1600, 2000) === 'close' && budgetStatus(2000, 2000) === 'close' && budgetStatus(1750, 2000) === 'close';\n"},{"id":"ok","test":"return budgetStatus(1599, 2000) === 'ok' && budgetStatus(0, 2000) === 'ok';\n"},{"id":"label","test":"return amountLabel(12500) === 'big' && amountLabel(10000) === 'big' && amountLabel(9999) === 'small' && amountLabel(0) === 'small';\n"}],"title":"Is Pocket on budget?","prompt":"<p>Fill in <code dir=\"ltr\">budgetStatus(spentCents, budgetCents)</code> so it returns <code dir=\"ltr\">&quot;over&quot;</code> when spending is <strong>more than</strong> the budget, <code dir=\"ltr\">&quot;close&quot;</code> when it is at least 80% of the budget (but not over), and <code dir=\"ltr\">&quot;ok&quot;</code> otherwise. Spending exactly the budget counts as <code dir=\"ltr\">&quot;close&quot;</code>, not over.</p>\n<p>Then fill in <code dir=\"ltr\">amountLabel(amountCents)</code> with a <strong>ternary</strong>: return <code dir=\"ltr\">&quot;big&quot;</code> for 10000 cents ($100) or more, and <code dir=\"ltr\">&quot;small&quot;</code> otherwise. Writing <code dir=\"ltr\">return</code> followed by a value hands that value back to whoever called the function.</p>\n","hints":["Check the narrowest case first. Over budget is also “at least 80%”, so test <code dir=\"ltr\">spentCents &gt; budgetCents</code> before the 80% check.","80% of the budget is <code dir=\"ltr\">budgetCents * 0.8</code>. Use <code dir=\"ltr\">&gt;=</code> so that exactly 80% counts as close.","The ternary reads <code dir=\"ltr\">condition ? valueIfTrue : valueIfFalse</code>, so: <code dir=\"ltr\">return amountCents &gt;= 10000 ? &quot;big&quot; : &quot;small&quot;;</code>."],"explanation":"<p>The chain checks <code dir=\"ltr\">spentCents &gt; budgetCents</code> first because it is the narrowest condition: every over-budget amount is also at least 80% of the budget, so checking the 80% rule first would label overspending as merely “close”. Using <code dir=\"ltr\">&gt;</code> for “over” and <code dir=\"ltr\">&gt;=</code> for “close” makes the boundaries exact: 2000 of 2000 is close, 2001 is over, and 1600 (exactly 80%) is close. The ternary in <code dir=\"ltr\">amountLabel</code> picks one of two values and returns it directly, which is the job it was designed for.</p>\n","checkLabels":{"over":"Returns “over” when spending is more than the budget","close":"Returns “close” from 80% up to and including the budget","ok":"Returns “ok” below 80%","label":"<code dir=\"ltr\">amountLabel</code> returns “big” from 10000 cents and “small” below"}},"words":720,"readMinutes":3}