JS enginecountcount.htmlClicks: 01<button>Clicks: 0</button>2<script src="count.js"></script>1let count = 0;2const btn = document.querySelector("button");3btn.addEventListener("click", () => {4 count = count + 1;5 btn.textContent = "Clicks: " + count;6 console.log(count);7});Console1count.js:62count.js:601222
Back: the browser (its JavaScript engine, memory, and the window showing the page)Front: the two files, count.html and count.js, and the developer consoleTokens: the script, instructions, the click event and the values 0, 1 and 2At the back, the browser's JavaScript engine and one memory box, count, and the browser window showing count.html. In front, the two files as cards and the developer console. The script tag hands count.js to the engine, which runs it once: count = 0, find the button, listen for clicks. Then it waits. Each click is an event: the function runs, count goes up, the button's text changes and the console prints the count. Browser engines compile JavaScript to fast machine code as it runs (for example V8 in Chrome and Node.js, SpiderMonkey in Firefox, JavaScriptCore in Safari). The result is the same as running each statement in order, which is how the figure shows it.

The language of the web

  1. JavaScript is the programming language of the web. Every web browser has a JavaScript engine built in, which runs it. Node.js runs the same language outside the browser, on servers and laptops.

Loading the page

Running the script

Reacting to a click

What happens

// step 1/11 · language
JavaScript   the programming language of web pages
runs in      every web browser (its JavaScript engine)
             and outside the browser with Node.js
used for     making pages react to you
standard     ECMAScript (ECMA-262)

Browser engines compile JavaScript to fast machine code as it runs (for example V8 in Chrome and Node.js, SpiderMonkey in Firefox, JavaScriptCore in Safari). The result is the same as running each statement in order, which is how the figure shows it. The named box is a simplification: count is a binding in the script's scope that refers to the number value. The button stored in btn is a reference to the DOM object, not a copy of it.