How JavaScript Runs
A JavaScript engine executes the language. Its surrounding runtime supplies capabilities such as a browser document or Node.js file access. Chromium and Node.js both use the V8 engine.
The same engine can calculate 2 + 3 in two places. What it can do with that result depends on the tools supplied by its environment.
First, separate the engine from its surroundings
JavaScript source is text containing instructions. Something must read and execute those instructions.
- The engine parses, compiles and executes the JavaScript language. Here we will follow V8.
- The runtime surrounds that engine with tools the program can call. A browser provides a page document; Node.js provides tools such as filesystem access.
An engine and a runtime are therefore different things. Sharing V8 does not mean sharing every API.
In the browser: Chromium, Blink and V8
We will use a Chromium-based browser, such as Chrome, for this explanation. Other browser families can use different engines.
Inside this browser, Blink handles HTML/CSS document and rendering responsibilities, while V8 executes JavaScript. The browser also supplies web APIs such as the page's document. V8 does not draw a heading just because it calculated a number.
HTML contains the heading and a script→Creates the document and supplies script source→Parses and compiles JavaScriptFollow this small page from source to visible result:
- HTML creates a heading whose initial words are Waiting for JavaScript.
- The browser passes the script to its JavaScript engine. The script appears after the heading here, so that element already exists.
- V8 evaluates
2 + 3, producing5. - The script calls browser-provided APIs to find the heading and assign its text. The browser can then render 5 in the page.
document.querySelector("#result") means “find the element with this id”; .textContent = ... replaces its displayed text. You will study these operations in the DOM module. Here, follow which component supplies each capability.
Predict and test: Change only 2 + 3 to 2 + 4, update the preview, and check the heading. Then remove the script: the HTML heading remains, but its original words are no longer replaced.
On your computer: Node.js and V8
Node.js can start JavaScript without a browser tab. It embeds V8 and supplies its own host tools, including filesystem and networking APIs. Node can run locally and on servers; it is not restricted to a developer's laptop. A browser can also display local content—the distinction is the environment, not whether the computer is “local.”
node app.cjs→Loads the file into a runtime process→Parses and compiles its JavaScriptWith Node installed, create these two files in the same folder.
article.txt
Quiet morningsapp.cjs
const fs = require("node:fs");
console.log(2 + 3);
console.log(typeof document);
console.log(fs.readFileSync("article.txt", "utf8").trim());From that folder, start the program:
node app.cjsThe terminal should show:
5
undefined
Quiet morningsThe first line is the same language calculation. The second uses typeof to inspect document: an ordinary Node process does not have a browser page document by default. The last line uses a Node-provided file API to read article.txt and print its text.
require("node:fs") asks Node for its filesystem tools. readFileSync reads the named file as text before continuing. You do not need to memorize module-loading syntax yet; the module is the source of this capability, and V8 is the engine executing the instructions. File access is subject to the process's permissions.
Predict and test: Change the text in article.txt and rerun the same JavaScript. Only the last output changes. The data came from the file, not from a title hard-coded inside V8.
Compare the two environments
| Responsibility | Chromium page | Node.js process |
|---|---|---|
| Execute JavaScript | V8 | V8 |
Calculate 2 + 3 |
Produces 5 | Produces 5 |
Supply the page's document |
Browser provides it | No page document by default |
| Render HTML and CSS | Browser/Blink rendering system | No browser renderer by default |
Read our local article.txt through node:fs |
That Node module is not a page API | Node provides the module |
| Coordinate asynchronous work | Browser scheduling and web APIs | Node scheduling, libuv and operating-system facilities |
V8 may interpret compiled bytecode and optimize code as it runs; “JavaScript runs” does not mean one fixed translation step happens identically for every function. For asynchronous work, the host coordinates completion and schedules JavaScript to resume. We will trace those queues in the event-loop module rather than assume every operation runs on a separate JavaScript thread.
Where this course's console editor fits
The HTML preview above supplies an actual document. The console editor below instead uses an isolated browser worker. It uses your browser's JavaScript engine and has no page document. It is not a local Node process, and the browser editor does not expose node:fs or your laptop's files.
Expect 5, then undefined. The missing document does not identify the runtime by itself: both this browser worker and an ordinary Node process can lack it. Use the environment and its documented capabilities to explain the result.
In the practice below, separate what the language computes from what its host makes possible.