What JavaScript is and how it works
JavaScript is a programming language standardised as ECMAScript: it is used to make pages interactive in the browser and to write servers in Node.js. The language is the same everywhere; the environment the code runs in provides access to the page, the network and files.
JavaScript is essential whenever a page has to respond to user actions: showing a menu, validating a form, loading data without a reload. A modern browser is enough to run such code. Outside the browser, the same language runs in Node.js — on servers and in development tools. Code runs on a single thread, so a long computation on a page freezes the interface: such tasks are moved to a Web Worker. JavaScript does not check that a variable is only ever assigned values of one type: if you need that check before the code runs, you use TypeScript.
Running JavaScript is a joint effort of two programs. The engine parses the source code and executes it: it maintains the call stack and the heap, where objects live. These are V8 in Chrome and Node.js, SpiderMonkey in Firefox, JavaScriptCore in Safari. The host environment — the browser or Node.js — connects the code to the outside world. The browser provides the DOM, timers and fetch(); Node.js provides file and network access. When a timer fires or a response arrives, the callback goes into the task queue, and the event loop hands it to the engine as soon as the call stack is empty.
Where to run code
To try things out, the browser console in DevTools works well: it runs the code you enter and immediately shows the result of the last expression. On a page, a script is attached with a <script> element; the type="module" attribute makes the file an ES module — ES modules are covered in the “ES modules” section. Outside the browser, the file is run with the node command.
<script type="module" src="app.js"></script>node app.jsHow JavaScript differs from TypeScript, Node.js and Java
| Name | What it is | How it relates to JavaScript |
|---|---|---|
| ECMAScript | the language standard (ECMA-262) | JavaScript implements ECMAScript; the names are often used as synonyms |
| TypeScript | a typed superset of the language | working JavaScript code is already valid TypeScript code; type errors show up before the code runs |
| Node.js | a runtime | runs JavaScript outside the browser on the V8 engine; files and networking instead of the DOM |
| Java | a different language | similar in expression syntax, but built on classes and static typing |
Choose TypeScript if you want the editor to catch type errors before the code runs; choose Node.js when you need JavaScript on the server, in scripts and in tools.
What to know beforehand
- HTML: elements, attributes and how they nest — the “HTML and CSS” reference
- Programming basics in any language: variables, conditionals, loops
Section sources
the section is written from these pages- JavaScript execution modeldeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model(opens in a new tab)
- Introductiondeveloper.mozilla.org/en-US/docs/Web/JavaScript/Guide/Introduction(opens in a new tab)
- JavaScript technologies overviewdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/JavaScript_technologies_overview(opens in a new tab)
- TypeScript for JavaScript Programmerstypescriptlang.org/docs/handbook/typescript-in-5-minutes.html(opens in a new tab)
- Introduction to Node.jsnodejs.org/learn/getting-started/introduction-to-nodejs(opens in a new tab)