Compare two places where JavaScript can run:
- A browser page, where the host provides a document API.
- Node.js, where the host provides process information.
Both programs use the same JavaScript calculation. Their environment-specific APIs differ.
Why does adding two numbers work in both programs, while the browser page API in one program is unavailable in a regular Node.js script?
Copy this complete code block into a file named browser-example.html, then open it in a browser.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JavaScript in a Browser Runtime</title>
</head>
<body>
<main>
<h1>Browser runtime example</h1>
<p id="browser-result">Waiting for JavaScript...</p>
</main>
<script>
const result = 2 + 3;
const output = document.querySelector("#browser-result");
output.textContent = "Core language calculation: " + result;
console.log("Browser page result: " + result);
</script>
</body>
</html>The page displays:
Core language calculation: 5
The browser developer console displays:
Browser page result: 5
The addition and string combination use the JavaScript language. The document lookup and page update use APIs supplied by the browser.
Copy this code into a file named node-example.js.
const result = 2 + 3;
console.log("Core language calculation: " + result);
console.log("Runtime: Node.js");
console.log("Node.js version: " + process.version);Open a terminal in the folder containing the file and run:
node node-example.js
This requires Node.js to be installed. The next chapter explains how to prepare an editor, terminal, and project folder.
The first two lines are:
Core language calculation: 5
Runtime: Node.js
The third line shows the Node.js version installed on the computer, for example:
Node.js version: v<installed-version>
The exact version value depends on the installed Node.js release.
| Feature | Browser page | Node.js program |
|---|---|---|
| JavaScript engine | Supplied as part of the browser | V8 is used by Node.js |
| Runtime environment | Browser and web platform | Node.js |
| Host-specific API in this example | The document API updates the page | The process API reports the runtime version |
| Output location | Page and browser developer console | Terminal |
| Shared language work | Adds 2 and 3, combines values into text | Adds 2 and 3, combines values into text |
The JavaScript expression 2 + 3 means the same operation in both environments. The host-specific APIs differ.
The engine processes the source code and performs JavaScript language operations. The example does not need to know whether the engine uses an interpreter, compilation, or optimization internally.
The browser provides the document object used to find and update a page element. Node.js provides the process object used to inspect Node.js information.
The language and the host APIs work together, but they are separate parts of the execution environment.
In the browser example, the program is running in a browser runtime. The engine is one component of that browser. In the terminal example, Node.js is the runtime and it uses V8 as its JavaScript engine.
Application code usually relies on the runtime's documented APIs. It should not depend on private engine implementation details.
- Change the calculation in both examples from 2 + 3 to 10 + 4.
- Change the browser page text, then reload the page.
- Change the Node.js message, then run the terminal command again.
- Copy the browser code into a regular Node.js file. Notice that a page document is not available by default.
- Copy the Node.js process expression into a browser script. Notice that a browser page should not assume the Node.js process API exists.
- The browser page still says it is waiting: Check that the script block was copied and that the browser console has no syntax error.
- The browser reports that document is missing: Make sure the code is running in a browser page or an environment that supplies a document.
- The terminal says node is not recognized: Node.js may not be installed or the terminal may need to be restarted after installation. Setup is covered in the next chapter.
- The Node.js version differs from the example: This is expected. The program reports the version installed on your computer.
- One JavaScript language can run in different environments.
- A JavaScript engine executes the language, and a runtime supplies host features.
- The browser page API and Node.js process API come from different hosts.
- The same core calculation can work in both environments.
- The engine's internal execution strategy is not something an introductory program needs to manage.
| Previous chapter | Chapter lesson | Chapter Q&A | Next chapter |
|---|---|---|---|
| 02 - JavaScript on the web: HTML, CSS, and JavaScript roles | Open lesson | Open Q&A | 04 - Set up the editor, terminal, and project files |