JavaScript is the programming language that every browser understands. It is what turns a static HTML page into something that reacts to clicks, keystrokes, and network responses. This guide explains what the language actually is, which host environments run it, such as the browser, Node.js, and a few less obvious ones, and why the language and the engine that executes it are two separate things.
This is the first lesson in the JavaScript Basics for Beginners module, and the next lesson in this module is about "Adding JavaScript to a Web Page: Three Real Ways".
What is JavaScript, exactly?
JavaScript is a high-level, dynamically typed, garbage-collected programming language. High-level means that you never manage memory by hand. Dynamically typed means that the type of a variable follows the value that it holds, instead of following a type that you declare. Garbage-collected means that the engine reclaims memory once it can prove that you no longer use it, so there is no free() for you to call.
Notice what that definition leaves out. The core language, which covers variables, functions, arrays, objects, loops, and error handling, contains no browser at all. Save the next example as hello.js and it behaves in exactly the same way in Chrome, Firefox, Node.js, Deno, and Bun.
const greeting = "Hello from the JavaScript engine";
console.log(greeting);
console.log("2 + 2 =", 2 + 2);
What is browser-specific is the layer of APIs that sits on top of the language. There is document, window, fetch, and localStorage. Those are extra rooms in the building, and not the building itself. That separation is why you can learn JavaScript properly in a terminal and add the web APIs later, once a project actually needs them.
Where does JavaScript actually run?
JavaScript needs a host environment to run in. The language itself has no entry point, no file access, and no network stack. The host supplies all of that. In practice you will meet five kinds of host.
| Host | What it adds to JavaScript | Typical use |
|---|---|---|
| Browser (Chrome, Firefox, Safari, Edge) | DOM, window, timers, fetch, storage, canvas, WebAssembly | Web pages and web apps |
| Node.js | File system, process, npm packages, HTTP server APIs | Servers, scripts, command-line tools, build tooling |
| Deno and Bun | A Node-like API surface with modern defaults, such as built-in TypeScript support and a bundler | Modern server and scripting work |
| Web Workers and Service Workers | A thread with no DOM, plus offline caching and background sync | Heavy computation and offline-first apps |
| Embedded engines (Electron, React Native) | Native operating-system calls that are exposed as JavaScript APIs | Desktop and mobile applications |
You can detect which host you are in while the program runs, because each host adds a different set of global names.
console.log("globalThis available:", typeof globalThis !== "undefined");
console.log("running in a browser:", typeof document !== "undefined");
console.log("running under Node.js:", typeof process !== "undefined");
if (typeof process !== "undefined") {
console.log("runtime version:", process.versions.node);
}
Run that with node where-am-i.js and the browser row prints false while the Node row prints true. Open the same file in a browser and the two answers swap over. The version line is wrapped in a check because process does not exist in the browser, and a bare process.versions.node there throws a ReferenceError straight away.
Is JavaScript the language or the engine?
JavaScript is the language, which means the syntax and the rules that you write. The engine is the program that reads your source code and executes it. V8 (Chrome, Edge, and Node.js), SpiderMonkey (Firefox), and JavaScriptCore (Safari) are three separate engines. They are free to differ in speed and in how quickly they accept new proposals, as long as they all agree on the standard.
That standard is called ECMAScript. ECMAScript is the specification, and JavaScript is the language that people actually write. The two terms have drifted so far apart that the versions are named after the specification, so you will hear ES2023 and ES2024, while the everyday word stays JavaScript. When you read that a feature is "ES2022", the real question is not whether it counts as JavaScript, but whether your target engine has caught up with it yet.
// Core language features run identically in every host.
function fibonacci(n) {
let previous = 0;
let current = 1;
for (let i = 2; i <= n; i += 1) {
const next = previous + current;
previous = current;
current = next;
}
return current;
}
console.log("fibonacci(10) =", fibonacci(10));
The function above touches nothing that is host-specific. Its output, fibonacci(10) = 55, is the same on a phone, on a server, and on a laptop, because nothing about it depends on where it landed.
Is JavaScript related to Java?
No it is not. The names collide, and essentially nothing else about the two languages does. Java is a compiled, statically typed, class-based language that was designed to run on a virtual machine. JavaScript is interpreted, dynamically typed, and built on prototypes. Brendan Eich created it at Netscape in 1995.
The confusion has a single historical cause that is worth remembering. The browser that first shipped JavaScript also had a Java applets runtime, and the marketing borrowed the more familiar word. So if your notes say that Java is a subset of JavaScript, or that JavaScript is a stripped-down Java, then both of those are wrong, and both of them trace back to that naming accident instead of to the languages themselves.
What happens between writing code and seeing it work?
There are roughly three stages. The engine reads the source text, it parses that text into a structure it can walk, and then it executes the structure. Parsing is where syntax errors are caught. A missing bracket produces a SyntaxError before a single statement of your program runs, which is why one typo near the bottom of a file can stop the top of it from working at all.
Execution is not a march from the top of the file to the bottom. The engine keeps a stack of what it is currently doing, and that is how it knows which function to return into. It also keeps a queue of work that is waiting to happen, such as timers, network callbacks, and click handlers. The event loop is the mechanism that moves finished work from that queue onto the stack. Code inside a callback therefore runs later, after the current block has finished, no matter where it appears in the file.
How is JavaScript different from HTML and CSS?
There are three jobs, and each one has its own constraints. HTML supplies structure and meaning. CSS supplies presentation. JavaScript supplies behaviour, and because it runs while the page is live, it can change the structure and the presentation after the fact.
The constraint that catches beginners out is this one. A script cannot rewrite the page wholesale. The browser parses your HTML into a live tree of nodes, and your script edits that tree one node at a time. There is no whole document object that you can replace in one go. That is also why where you put your script tag in the HTML matters, and that is the subject of the next lesson.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Where am I?</title>
</head>
<body>
<p>Open the console and read the output.</p>
<script src="where-am-i.js"></script>
</body>
</html>
What to carry forward
- JavaScript is the language, ECMAScript is the specification that defines it, and V8, SpiderMonkey, and JavaScriptCore are the engines that run it.
- The core language does not depend on any host. The DOM,
window, andfetchare host extras that you opt into on purpose. - Node.js gives you the same language plus a file system, a package ecosystem, and a process object. That is the whole reason server-side JavaScript exists.
- A syntax error kills the entire file. A runtime error kills only the statement that hit it, and everything after it may still run.
Try it yourself: Make a folder, put the host-detection snippet above into a file named where-am-i.js, and run node where-am-i.js. Note which two lines print true. Next, copy that file into a page that loads it with a <script src="where-am-i.js"> tag at the end of <body>, open it in a browser, and confirm that the browser and Node rows swap over. One file, two hosts, and two different answers is runtime detection working.