{"id":"l-js-1-1","courseId":"javascript-fundamentals","lang":"en","kind":"intro","summary":"Know what JavaScript is, where it runs (the browser and Node.js), how to run your first line of code, and what you will build in this course.","html":"<p>Every website you used today ran JavaScript. When a “Like” counter goes up without the page reloading, when a form tells you your password is too short before you press Send, when a map scrolls under your finger, that is JavaScript running inside your browser. It is the only programming language every browser understands, which is why it is the best first language for anyone who wants to build for the web.</p>\n<p>You are going to learn it the slow, solid way: by watching what the computer actually does with each line you write.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">One language, two homes</h2>\n<p>JavaScript is a language: a set of rules for writing instructions. Something has to read those instructions and carry them out. That something is a <strong>JavaScript engine</strong>. Chrome and Edge use one called V8, Firefox uses SpiderMonkey, Safari uses JavaScriptCore. They all follow the same official standard, called ECMAScript, which gets a new edition every year.</p>\n<p>An engine never runs alone. It lives inside a <strong>host</strong> that gives your code extra abilities:</p>\n<ul>\n<li>In the <strong>browser</strong>, the host adds the page itself (<code dir=\"ltr\">document</code>), clicks and key presses, the network (<code dir=\"ltr\">fetch</code>) and a little storage (<code dir=\"ltr\">localStorage</code>).</li>\n<li>In <strong>Node.js</strong>, the same V8 engine runs on a computer or server with no page at all. Instead, the host adds files, servers and command-line tools.</li>\n</ul>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 640 250\" role=\"img\" aria-labelledby=\"l-js-1-1-t1\"><defs><marker id=\"arrow-l-js-1-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-1-1-t1\">Your JavaScript code runs on a JavaScript engine. In the browser the engine is surrounded by the page, events, fetch and localStorage; in Node.js it is surrounded by files, servers and the command line.</title>\n  <rect class=\"d-box-primary\" x=\"230\" y=\"14\" width=\"180\" height=\"48\" rx=\"10\"/>\n  <text class=\"d-label-strong\" x=\"320\" y=\"44\" text-anchor=\"middle\">Your JavaScript</text>\n  <path class=\"d-arrow\" d=\"M285 62 L165 104\" marker-end=\"url(#arrow-l-js-1-1-1)\"/>\n  <path class=\"d-arrow\" d=\"M355 62 L475 104\" marker-end=\"url(#arrow-l-js-1-1-1)\"/>\n  <rect class=\"d-box\" x=\"30\" y=\"108\" width=\"270\" height=\"128\" rx=\"12\"/>\n  <text class=\"d-label-strong\" x=\"165\" y=\"132\" text-anchor=\"middle\">Browser</text>\n  <rect class=\"d-box-accent\" x=\"105\" y=\"144\" width=\"120\" height=\"34\" rx=\"8\"/>\n  <text class=\"d-label\" x=\"165\" y=\"166\" text-anchor=\"middle\">JS engine</text>\n  <text class=\"d-label-muted\" x=\"165\" y=\"204\" text-anchor=\"middle\">+ page, events,</text>\n  <text class=\"d-label-muted\" x=\"165\" y=\"224\" text-anchor=\"middle\">fetch, localStorage</text>\n  <rect class=\"d-box\" x=\"340\" y=\"108\" width=\"270\" height=\"128\" rx=\"12\"/>\n  <text class=\"d-label-strong\" x=\"475\" y=\"132\" text-anchor=\"middle\">Node.js</text>\n  <rect class=\"d-box-accent\" x=\"415\" y=\"144\" width=\"120\" height=\"34\" rx=\"8\"/>\n  <text class=\"d-label\" x=\"475\" y=\"166\" text-anchor=\"middle\">JS engine</text>\n  <text class=\"d-label-muted\" x=\"475\" y=\"204\" text-anchor=\"middle\">+ files, servers,</text>\n  <text class=\"d-label-muted\" x=\"475\" y=\"224\" text-anchor=\"middle\">command line</text>\n</svg><figcaption>The same language runs in two hosts with different extras</figcaption></figure>\n<p>That split matters from day one. Everything in sections 1 to 3 of this course is the language itself, so it works in both places. Section 4 uses browser extras, so that code only runs on a web page.</p>\n<h2 id=\"sec-2\" tabindex=\"-1\">Your first line of code</h2>\n<p>Here is a complete JavaScript program. Press Run.</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:#0A3069;--shiki-dark:#A5D6FF\">\"Pocket is running\"</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:#0550AE;--shiki-dark:#79C0FF\">450</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> +</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 1200</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>Two things happened, in order. First the engine read line 1, saw an instruction to log some text, and printed it. Then it read line 2, worked out <code dir=\"ltr\">450 + 1200</code>, and printed the result, <code dir=\"ltr\">1650</code>. Each of these lines is a <strong>statement</strong>, one instruction, usually ended with a semicolon. The engine runs statements top to bottom, one at a time, and finishes each before starting the next.</p>\n<p><code dir=\"ltr\">console.log()</code> is how you see inside your program. You will use it constantly, and professional developers do too. Anything between the parentheses is worked out first, and the result is printed.</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>Three places to run JavaScript</span></p>\n<ol>\n<li>The Run buttons and exercises in this course, which work in your browser with nothing to install.</li>\n<li>Your browser’s console: open any page, press Cmd+Option+J on a Mac or Ctrl+Shift+J on Windows in Chrome, type <code dir=\"ltr\">1 + 1</code> and press Enter.</li>\n<li>Node.js on your computer: install the current LTS version from nodejs.org, save code in <code dir=\"ltr\">hello.js</code>, and run <code dir=\"ltr\">node hello.js</code> in a terminal.</li>\n</ol>\n</aside>\n<h2 id=\"sec-3\" tabindex=\"-1\">What you will build: Pocket</h2>\n<p>Throughout this course you build one program, a little at a time: <strong>Pocket</strong>, an expense tracker. In the next lessons it is a handful of variables printed to the console. By section 3 it holds a list of expenses, totals them by category and sorts them. In section 4 it becomes a web page with a form, a live list and saved data. In section 5 you organise it the way real projects are organised, with modules, classes and proper error handling.</p>\n<p>Most lessons end with an exercise. You write code in the editor, press Check, and a set of automatic checks tells you what works and what does not yet. Getting a check wrong is normal and useful: read the message, change one thing, and try again. That loop of write, run, read and fix is what programming feels like every day.</p>\n<p>Next you meet the raw material every program works with: values, their types, and the variables that hold them.</p>\n","toc":[{"id":"sec-1","text":"One language, two homes","level":2},{"id":"sec-2","text":"Your first line of code","level":2},{"id":"sec-3","text":"What you will build: Pocket","level":2}],"takeaways":["JavaScript is a programming language that every web browser can run, and Node.js runs the same language outside the browser.","The language itself is small; the host it runs in (browser or Node.js) adds extras such as the page, files or the network.","Code runs one statement at a time, top to bottom, unless you tell it to do otherwise.","<code dir=\"ltr\">console.log()</code> prints a value so you can see what your program is doing; it is the first debugging tool you reach for."],"further":[{"title":"What is JavaScript? (MDN)","url":"https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/What_is_JavaScript"},{"title":"Introduction to Node.js","url":"https://nodejs.org/en/learn/getting-started/introduction-to-nodejs"},{"title":"console.log() (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/API/console/log_static"}],"quiz":[{"q":"<p>You write <code dir=\"ltr\">console.log(2 + 3)</code> on line 1 and <code dir=\"ltr\">console.log(&quot;done&quot;)</code> on line 2. What appears in the console, and in what order?</p>","options":[{"text":"<code dir=\"ltr\">5</code> and then <code dir=\"ltr\">done</code>, because statements run top to bottom.","why":"Correct. The first statement is evaluated and printed, then the second one."},{"text":"<code dir=\"ltr\">done</code> and then <code dir=\"ltr\">5</code>, because text is printed before numbers.","why":"JavaScript does not group output by type. Statements run in the order they are written."},{"text":"<code dir=\"ltr\">2 + 3</code> and then <code dir=\"ltr\">done</code>, because <code dir=\"ltr\">console.log</code> prints exactly what you typed.","why":"The expression <code dir=\"ltr\">2 + 3</code> is evaluated first, so <code dir=\"ltr\">console.log</code> receives the number 5, not the characters you typed."}],"answer":0},{"q":"<p>Which of these is something the browser adds, rather than the JavaScript language itself?</p>","options":[{"text":"Numbers and the <code dir=\"ltr\">+</code> operator.","why":"Numbers and operators are part of the language, so they work the same in Node.js and in every browser."},{"text":"The <code dir=\"ltr\">if</code> statement.","why":"Control flow such as <code dir=\"ltr\">if</code> is core language syntax and exists in every host."},{"text":"The <code dir=\"ltr\">document</code> object that lets you change the page.","why":"Correct. <code dir=\"ltr\">document</code> is a browser API. Node.js has no page, so there is no <code dir=\"ltr\">document</code> there."}],"answer":2},{"q":"<p>A friend says “JavaScript is a simpler version of Java.” What is the most accurate reply?</p>","options":[{"text":"They are unrelated languages that share part of a name for marketing reasons in 1995.","why":"Correct. JavaScript was named to ride on Java’s popularity at the time. The two languages work very differently."},{"text":"JavaScript is the browser edition of Java and compiles to the same bytecode.","why":"JavaScript is not compiled to Java bytecode and has its own standard, ECMAScript."},{"text":"JavaScript is an older language that Java later replaced on the web.","why":"Java applets disappeared from the web; JavaScript is the language every browser runs today."}],"answer":0}],"exercise":{"kind":"web","mode":"js","starter":{"js":"// 1. Log the text: Welcome to Pocket\n// 2. Log the result of adding three expenses: 450, 1200 and 899\n"},"solution":{"js":"console.log(\"Welcome to Pocket\");\nconsole.log(450 + 1200 + 899);\n"},"checks":[{"id":"logs-welcome","test":"return logs.some((l) => l.toLowerCase().includes('welcome to pocket'));"},{"id":"logs-total","test":"return logs.some((l) => l.trim() === '2549');"},{"id":"welcome-first","test":"const w = logs.findIndex((l) => l.toLowerCase().includes('welcome to pocket'));\nconst t = logs.findIndex((l) => l.trim() === '2549');\nreturn w !== -1 && t !== -1 && w < t;\n"}],"title":"Say hello to Pocket","prompt":"<p>Write a two-line program. First log the text <code dir=\"ltr\">Welcome to Pocket</code>. Then log the total of three expenses, <code dir=\"ltr\">450</code>, <code dir=\"ltr\">1200</code> and <code dir=\"ltr\">899</code>, by letting JavaScript do the addition inside <code dir=\"ltr\">console.log()</code>.</p>\n","hints":["Text goes inside quotes, <code dir=\"ltr\">&quot;like this&quot;</code>. Numbers do not need quotes.","You can put a whole calculation inside the parentheses, for example <code dir=\"ltr\">console.log(1 + 2)</code>.","<code dir=\"ltr\">console.log(450 + 1200 + 899);</code> prints the total."],"explanation":"<p>The engine runs your two statements in order. In the first, <code dir=\"ltr\">&quot;Welcome to Pocket&quot;</code> is text, so it is printed as it is. In the second, JavaScript evaluates <code dir=\"ltr\">450 + 1200 + 899</code> first, gets <code dir=\"ltr\">2549</code>, and only then hands that single number to <code dir=\"ltr\">console.log()</code>. Working out what is inside the parentheses before using it is a rule you will lean on in every lesson.</p>\n","checkLabels":{"logs-welcome":"Logs “Welcome to Pocket”","logs-total":"Logs the total 2549","welcome-first":"Logs the welcome before the total"}},"words":616,"readMinutes":3}