{"id":"l-js-1-2","courseId":"javascript-fundamentals","lang":"en","kind":"lesson","summary":"Recognise JavaScript’s basic value types, check them with typeof, and store values in variables with const and let, choosing the right one each time.","html":"<p>Pocket needs to remember things: the app’s name, how much you have spent today, whether you are over budget. A program remembers by keeping <strong>values</strong> and giving them <strong>names</strong>. Before you can do anything interesting, you need to know what kinds of values exist and how to name them.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">Values and their types</h2>\n<p>A value is a single piece of data. Every value has a <strong>type</strong>, which decides what you can do with it. These five cover almost everything you will touch in your first weeks:</p>\n<div class=\"table-wrap\" role=\"region\" aria-label=\"Scrollable table\" tabindex=\"0\"><table><thead>\n<tr>\n<th>Type</th>\n<th>Examples</th>\n<th>Used for</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>number</td>\n<td><code dir=\"ltr\">42</code>, <code dir=\"ltr\">4.5</code>, <code dir=\"ltr\">-3</code></td>\n<td>amounts, counts, measurements</td>\n</tr>\n<tr>\n<td>string</td>\n<td><code dir=\"ltr\">&quot;Coffee&quot;</code>, <code dir=\"ltr\">'food'</code></td>\n<td>any text</td>\n</tr>\n<tr>\n<td>boolean</td>\n<td><code dir=\"ltr\">true</code>, <code dir=\"ltr\">false</code></td>\n<td>yes/no answers</td>\n</tr>\n<tr>\n<td>undefined</td>\n<td><code dir=\"ltr\">undefined</code></td>\n<td>“no value has been given yet”</td>\n</tr>\n<tr>\n<td>null</td>\n<td><code dir=\"ltr\">null</code></td>\n<td>“deliberately empty”</td>\n</tr>\n</tbody>\n</table></div><p>The <code dir=\"ltr\">typeof</code> operator tells you the type of any value. Run this and compare each line with the table:</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:#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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 42</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"Coffee\"</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> true</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> undefined</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> null</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span></code></pre><div class=\"run-output\" data-run-output=\"0\" hidden></div></div>\n<p>Four answers match the table. The last one says <code dir=\"ltr\">&quot;object&quot;</code>, which is a bug from 1995 that can never be fixed because millions of websites depend on it. When you need to know whether something is <code dir=\"ltr\">null</code>, compare it directly: <code dir=\"ltr\">value === null</code>.</p>\n<p>There are two more basic types, bigint (for huge whole numbers) and symbol (for unique labels), which you will rarely need as a beginner. Everything else, such as lists and records, is an <strong>object</strong>, and section 3 is all about those.</p>\n<h2 id=\"sec-2\" tabindex=\"-1\">Naming values with const and let</h2>\n<p>A <strong>variable</strong> is a name that points at a value. You create one with a <strong>declaration</strong>:</p>\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\"> appName</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"Pocket\"</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\"> spentToday </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>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">spentToday </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spentToday </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 4.5</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">spentToday </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spentToday </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 12</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></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\">(appName, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"spent today:\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, spentToday);</span></span></code></pre><div class=\"run-output\" data-run-output=\"1\" hidden></div></div>\n<p>Read line 4 the way the engine does: first it evaluates the right side, <code dir=\"ltr\">spentToday + 4.5</code>, which is <code dir=\"ltr\">0 + 4.5</code>, so <code dir=\"ltr\">4.5</code>. Then it points the name <code dir=\"ltr\">spentToday</code> at that new value. The <code dir=\"ltr\">=</code> sign means “point this name at”, not “is equal to”. That is why <code dir=\"ltr\">x = x + 1</code> makes sense in code even though it is nonsense in maths.</p>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 640 210\" role=\"img\" aria-labelledby=\"l-js-1-2-t1\"><defs><marker id=\"arrow-l-js-1-2-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-1-2-t1\">The name appName points to the string Pocket and can never move. The name spentToday first points to 0, then 4.5, then 16.5, each reassignment moving the arrow to a new value.</title>\n  <rect class=\"d-box-primary\" x=\"20\" y=\"30\" width=\"150\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"95\" y=\"57\" text-anchor=\"middle\">const appName</text>\n  <path class=\"d-arrow\" d=\"M170 52 L250 52\" marker-end=\"url(#arrow-l-js-1-2-1)\"/>\n  <rect class=\"d-box\" x=\"254\" y=\"30\" width=\"120\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"314\" y=\"57\" text-anchor=\"middle\">\"Pocket\"</text>\n  <rect class=\"d-box-primary\" x=\"20\" y=\"130\" width=\"150\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"95\" y=\"157\" text-anchor=\"middle\">let spentToday</text>\n  <rect class=\"d-box\" x=\"254\" y=\"130\" width=\"80\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"294\" y=\"157\" text-anchor=\"middle\">0</text>\n  <rect class=\"d-box\" x=\"364\" y=\"130\" width=\"80\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"404\" y=\"157\" text-anchor=\"middle\">4.5</text>\n  <rect class=\"d-box-success\" x=\"474\" y=\"130\" width=\"90\" height=\"44\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"519\" y=\"157\" text-anchor=\"middle\">16.5</text>\n  <path class=\"d-dashed\" d=\"M170 140 L254 140\"/>\n  <path class=\"d-dashed\" d=\"M170 148 Q290 110 364 140\"/>\n  <path class=\"d-arrow\" d=\"M170 162 Q340 205 474 162\" marker-end=\"url(#arrow-l-js-1-2-1)\"/>\n  <text class=\"d-label-muted\" x=\"520\" y=\"110\" text-anchor=\"middle\">current value</text>\n</svg><figcaption>Reassigning a let moves the name to a new value</figcaption></figure>\n<p><code dir=\"ltr\">const</code> creates a name that can never be pointed anywhere else. <code dir=\"ltr\">let</code> creates one that can. Use this rule: <strong>declare with <code dir=\"ltr\">const</code> by default, and change it to <code dir=\"ltr\">let</code> only when you find you need to reassign.</strong> Most names in real programs never change, and a <code dir=\"ltr\">const</code> tells the next reader that they can stop worrying about it.</p>\n<p>You will also see a third keyword, <code dir=\"ltr\">var</code>, in older tutorials and Stack Overflow answers. It is the original way to declare variables and has scoping rules that cause bugs (you will see why in the scope lesson). Modern code does not use it.</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>Reassigning a const</span></p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-index=\"2\"><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\"> total</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>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 4.5</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// TypeError: Assignment to constant variable.</span></span></code></pre></div>\n<p>The program stops at this line. If a value really has to change, declare it with <code dir=\"ltr\">let</code>. If you meant to keep the old value and make a new one, give the new one its own name: <code dir=\"ltr\">const newTotal = total + 4.5;</code>.</p>\n</aside>\n<h2 id=\"sec-3\" tabindex=\"-1\">The value has the type, not the variable</h2>\n<p>In JavaScript a variable does not have a type of its own. It points at a value, and the value has a type. So a <code dir=\"ltr\">let</code> can point at a number now and a string later:</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\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> note </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 12</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> note);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">note </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"twelve dollars\"</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> note);</span></span></code></pre><div class=\"run-output\" data-run-output=\"3\" hidden></div></div>\n<p>This flexibility is called <strong>dynamic typing</strong>. It is convenient, and it is also how many beginner bugs start: a number quietly becomes a string and your sum turns into <code dir=\"ltr\">&quot;1212&quot;</code>. The next two lessons show exactly when that happens.</p>\n<h2 id=\"sec-4\" tabindex=\"-1\">Names: the rules and the habits</h2>\n<p>The rules: a name can contain letters, digits, <code dir=\"ltr\">_</code> and <code dir=\"ltr\">$</code>, cannot start with a digit, and cannot be a reserved word such as <code dir=\"ltr\">const</code> or <code dir=\"ltr\">if</code>. Names are case-sensitive, so <code dir=\"ltr\">total</code> and <code dir=\"ltr\">Total</code> are two different variables.</p>\n<p>The habits, which every JavaScript codebase follows: write names in <strong>camelCase</strong> (<code dir=\"ltr\">spentToday</code>, <code dir=\"ltr\">monthlyBudget</code>), and make them say what the value means, not what type it is. <code dir=\"ltr\">spentToday</code> beats <code dir=\"ltr\">num1</code>; <code dir=\"ltr\">isOverBudget</code> beats <code dir=\"ltr\">flag</code>. Booleans usually start with <code dir=\"ltr\">is</code>, <code dir=\"ltr\">has</code> or <code dir=\"ltr\">can</code>.</p>\n<p>Two errors you will meet while practising:</p>\n<ul>\n<li><code dir=\"ltr\">ReferenceError: spent is not defined</code> means you used a name that was never declared, often a typo.</li>\n<li><code dir=\"ltr\">ReferenceError: Cannot access 'spent' before initialization</code> means you used a <code dir=\"ltr\">let</code> or <code dir=\"ltr\">const</code> on a line above its declaration. Declare first, use after.</li>\n</ul>\n<p>Next lesson you start doing arithmetic with your numbers, and you will find out why <code dir=\"ltr\">0.1 + 0.2</code> is not quite <code dir=\"ltr\">0.3</code>.</p>\n","toc":[{"id":"sec-1","text":"Values and their types","level":2},{"id":"sec-2","text":"Naming values with const and let","level":2},{"id":"sec-3","text":"The value has the type, not the variable","level":2},{"id":"sec-4","text":"Names: the rules and the habits","level":2}],"takeaways":["Every value has a type; the basic ones are number, string, boolean, undefined and null, and <code dir=\"ltr\">typeof</code> tells you which one you have.","A variable is a name that points at a value; the value has a type, the variable does not.","Declare with <code dir=\"ltr\">const</code> by default and switch to <code dir=\"ltr\">let</code> only when the name must point at a different value later.","Reassigning a <code dir=\"ltr\">const</code> throws a TypeError, and using a name before declaring it throws a ReferenceError."],"further":[{"title":"JavaScript data types and data structures (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Data_structures"},{"title":"let (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let"},{"title":"const (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const"}],"quiz":[{"q":"<p>What does this code print?</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\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 10</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 5</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:#CF222E;--shiki-dark:#FF7B72\">typeof</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total, total);</span></span></code></pre></div>\n","options":[{"text":"<code dir=\"ltr\">number 10</code>","why":"The second line computes <code dir=\"ltr\">10 + 5</code> and points <code dir=\"ltr\">total</code> at the result, so the old value 10 is gone."},{"text":"<code dir=\"ltr\">string 15</code>","why":"Both values are numbers and no quotes are involved, so the result is a number, not text."},{"text":"<code dir=\"ltr\">number 15</code>","why":"Correct. The right side is evaluated first (10 + 5), then <code dir=\"ltr\">total</code> is pointed at 15, which is a number."}],"answer":2},{"q":"<p>You store the name of the app, <code dir=\"ltr\">&quot;Pocket&quot;</code>, which never changes, and the number of expenses entered so far, which grows. Which declarations fit best?</p>","options":[{"text":"<code dir=\"ltr\">let</code> for both, so you never hit an error.","why":"That works, but it throws away a useful signal. <code dir=\"ltr\">const</code> tells the reader, and the engine, that the name never moves."},{"text":"<code dir=\"ltr\">const</code> for the app name and <code dir=\"ltr\">let</code> for the count.","why":"Correct. The name is fixed, so <code dir=\"ltr\">const</code>; the count is reassigned as it grows, so <code dir=\"ltr\">let</code>."},{"text":"<code dir=\"ltr\">const</code> for both, and change the count with <code dir=\"ltr\">count + 1</code>.","why":"Writing <code dir=\"ltr\">count + 1</code> alone computes a new number but stores it nowhere. Storing it back needs reassignment, which <code dir=\"ltr\">const</code> forbids."},{"text":"<code dir=\"ltr\">var</code> for both, because it works in every browser.","why":"<code dir=\"ltr\">let</code> and <code dir=\"ltr\">const</code> have worked in every browser for years. <code dir=\"ltr\">var</code> has confusing scope rules and is avoided in modern code."}],"answer":1},{"q":"<p>What does <code dir=\"ltr\">typeof null</code> return?</p>","options":[{"text":"<code dir=\"ltr\">&quot;null&quot;</code>","why":"That would be logical, but it is not what JavaScript does. The real answer is a historical bug kept for compatibility."},{"text":"<code dir=\"ltr\">&quot;undefined&quot;</code>","why":"<code dir=\"ltr\">undefined</code> and <code dir=\"ltr\">null</code> are two different values with different <code dir=\"ltr\">typeof</code> results."},{"text":"<code dir=\"ltr\">&quot;object&quot;</code>","why":"Correct. It is a bug from the first version of JavaScript that can never be fixed without breaking old websites. To test for null, write <code dir=\"ltr\">value === null</code>."}],"answer":2},{"q":"<p>Which line throws <code dir=\"ltr\">TypeError: Assignment to constant variable.</code>?</p>","options":[{"text":"<code dir=\"ltr\">const rate = 5; rate = 6;</code>","why":"Correct. <code dir=\"ltr\">const</code> creates a name that can never be pointed at another value, so the second statement fails."},{"text":"<code dir=\"ltr\">let rate = 5; rate = 6;</code>","why":"<code dir=\"ltr\">let</code> exists exactly so you can reassign. This runs fine and <code dir=\"ltr\">rate</code> ends up as 6."},{"text":"<code dir=\"ltr\">const rate = 5; const next = rate + 1;</code>","why":"This reads <code dir=\"ltr\">rate</code> without changing it and stores the result in a new name. Nothing is reassigned."}],"answer":0}],"exercise":{"kind":"web","mode":"js","starter":{"js":"const appName = \"Pocket\";\nconst monthlyBudget = 2000;\n\n// 1. Declare spent, starting at 0\n// 2. Add an expense of 45 to spent, then one of 120\n// 3. Declare remaining: the budget minus what was spent\n// 4. Log the type of remaining\n"},"solution":{"js":"const appName = \"Pocket\";\nconst monthlyBudget = 2000;\n\nlet spent = 0;\nspent = spent + 45;\nspent = spent + 120;\n\nconst remaining = monthlyBudget - spent;\nconsole.log(typeof remaining);\n"},"checks":[{"id":"spent-total","test":"return typeof spent === 'number' && spent === 165;"},{"id":"remaining-value","test":"return remaining === 1835;"},{"id":"logs-type","test":"return logs.some((l) => l.trim() === 'number');"},{"id":"budget-const","test":"try { monthlyBudget = 1; return false; } catch (e) { return e instanceof TypeError; }\n"}],"title":"Track what you have spent","prompt":"<p>Pocket has a <code dir=\"ltr\">monthlyBudget</code> of 2000. Declare a variable <code dir=\"ltr\">spent</code> that starts at <code dir=\"ltr\">0</code>, then reassign it twice to add an expense of <code dir=\"ltr\">45</code> and one of <code dir=\"ltr\">120</code>. Declare <code dir=\"ltr\">remaining</code> as <code dir=\"ltr\">monthlyBudget</code> minus <code dir=\"ltr\">spent</code>, and log the <strong>type</strong> of <code dir=\"ltr\">remaining</code>. Keep <code dir=\"ltr\">monthlyBudget</code> a <code dir=\"ltr\">const</code>.</p>\n","hints":["<code dir=\"ltr\">spent</code> has to change, so it cannot be a <code dir=\"ltr\">const</code>.","To add to a variable, compute the new value from the old one: <code dir=\"ltr\">spent = spent + 45;</code>.","<code dir=\"ltr\">typeof</code> goes in front of the value: <code dir=\"ltr\">console.log(typeof remaining);</code>."],"explanation":"<p><code dir=\"ltr\">spent</code> is declared with <code dir=\"ltr\">let</code> because it is pointed at a new number twice: <code dir=\"ltr\">0</code>, then <code dir=\"ltr\">45</code>, then <code dir=\"ltr\">165</code>. Each time, the right side is evaluated using the current value before the name moves. <code dir=\"ltr\">remaining</code> never changes after it is computed, so <code dir=\"ltr\">const</code> is the right choice, and <code dir=\"ltr\">typeof remaining</code> is <code dir=\"ltr\">&quot;number&quot;</code> because subtracting two numbers gives a number. Trying to reassign <code dir=\"ltr\">monthlyBudget</code> throws a <code dir=\"ltr\">TypeError</code>, which is exactly the protection <code dir=\"ltr\">const</code> is for.</p>\n","checkLabels":{"spent-total":"<code dir=\"ltr\">spent</code> ends at 165","remaining-value":"<code dir=\"ltr\">remaining</code> is 1835","logs-type":"Logs the type of <code dir=\"ltr\">remaining</code> (number)","budget-const":"<code dir=\"ltr\">monthlyBudget</code> is still a <code dir=\"ltr\">const</code> that cannot be reassigned"}},"words":705,"readMinutes":3}