What JavaScript Is and How It Runs in the Browser
What JavaScript really is: a dynamic, single-threaded language, how the engine compiles your code, the event loop, and why JS is not the DOM.
You opened DevTools to figure out why a button wasn't responding, read "Uncaught TypeError," and closed the console more lost than when you started. It happens. JavaScript is the language that makes a page react to a click, validates a form before it hits the server, and updates the cart without reloading anything — and yet almost nobody stops to understand what actually happens when the browser hits a .js file. The confusion gets worse because the name causes silly mistakes: JavaScript is not Java, never was, and the "Script" at the end is basically a 90s marketing joke.
This guide explains what JavaScript really is, how the browser runs your code (spoiler: it's not "line by line"), and why the difference between the language and the DOM is the confusion that trips up beginners the most.
What JavaScript is, without the folklore
JavaScript is a dynamic, single-threaded programming language, created in 1995 to run inside the browser. "Dynamic" here means types are resolved at runtime: you don't declare that x is a number, you just assign a value and the language figures out the rest. That's flexible, and it's also the source of half the bugs you'll ever write.
"Single-threaded" means there's a single execution queue — one thing at a time. That sounds like an absurd limitation for a language that has to handle clicks, network requests, and animations at once, but that's exactly where the event loop comes in, which we'll get to.
About the name: JavaScript has no technical relationship to Java. It was named that way because Java was trendy and Netscape wanted to ride the hype. The real specification for the language is called ECMAScript, and when someone says "ES2020" or "ES6" they're talking about a version of that spec. In practice everyone just says JavaScript and moves on.
let name = "Rafael"; // string
name = 42; // now it's a number, and nobody complains
const items = [1, 2, 3];
Blunt opinion: "vanilla JS" — plain JavaScript, no framework — still matters a lot. React, Vue, and Svelte are great, but they compile down to the same JavaScript that runs in the browser, and when something breaks, that's the level where you'll debug. People who understand the language underneath pick up any framework in days; people who only memorized React get stuck on the first weird error.
How the browser runs your JavaScript
When the browser hits a <script>, it hands the code to the JS engine — V8 in Chrome and Node, SpiderMonkey in Firefox, JavaScriptCore in Safari. The engine does not read your code "line by line like a dumb interpreter," even though that's the explanation that's been floating around forums since 2010.
What actually happens: the engine parses the text and produces an intermediate representation (bytecode). An interpreter starts executing that bytecode right away, so you don't wait. In parallel, a JIT (just-in-time) compiler watches which functions run many times — the "hot paths" — and recompiles those parts into optimized machine code. That's why a loop executed a million times gets faster over time: the engine figured out it was worth optimizing.
In other words, JavaScript today is compiled, just during execution rather than before it. Calling it an "interpreted language" isn't entirely wrong, but it misses the most interesting point and creates the false impression that it's always slow. It isn't.
Call stack, event loop, and the "asynchronous" thing
Here's the part that confuses almost everyone. Because the language is single-threaded, it has a single call stack: the pile of functions currently executing. If one function calls another, it pushes; when it returns, it pops. Simple.
The problem: if the call stack is busy, nothing else runs — not the user's click, not the screen redraw. If you do a calculation that hangs for three seconds, the page freezes for three seconds. Ever seen that "page is not responding"? That's the call stack stuck.
The way out is not doing the slow work right then. When you fire a request (fetch), schedule a setTimeout, or wait for a click, the waiting work is handed off to the browser, outside the call stack. When it finishes, the result goes into a callback queue. The event loop is the mechanism that keeps watching: "is the call stack empty? Then I'll grab the next callback from the queue and push it onto the stack." That dance is what lets a single-threaded language appear to do several things at once.
console.log("1");
setTimeout(() => console.log("3"), 0);
console.log("2");
// prints 1, 2, 3 — the setTimeout waits for the stack to empty,
// even with a delay of 0
This example trips up plenty of people in interviews. A setTimeout with zero delay doesn't run immediately: it goes to the queue and only executes when the call stack is free. Async/await and Promises are syntactic sugar on top of this same mechanism — more readable, same gears underneath.
JavaScript is not the DOM
This is the distinction worth gold. JavaScript, the language, knows about numbers, strings, arrays, functions, objects. That's it. It doesn't know what a button, a <div>, or a background color is.
Everything that touches the page — document.querySelector, element.classList.add, addEventListener — is not JavaScript proper. It's the DOM (Document Object Model), an API the browser exposes so your code can manipulate the page. The engine runs the language; the browser provides the bridge to the page. They're two separate layers, and that's why the same JavaScript runs outside the browser, where the DOM simply doesn't exist.
To understand how this entire page reaches the browser before JavaScript even enters the scene — the path from request to render — it's worth reading how a web page works, which covers the full loading pipeline.
Where else JavaScript runs
In 2009, Node.js took the V8 engine, pulled it out of the browser, and put it on the server. Suddenly you could write back-end code in the same language as the front-end. That's why JavaScript today runs on servers, build tools, automation scripts, serverless functions, and even microcontrollers. The language is the same; what changes are the available APIs — in the browser you have the DOM and fetch, in Node you have low-level access to the filesystem and the network.
A practical detail that affects performance: how you load the <script>. With no attribute, the browser stops building the page, downloads and runs the script, and only then continues — blocking. With defer, it downloads in parallel and runs only after the HTML has finished parsing, in the order they appear. With async, it downloads in parallel and runs as soon as it's ready, with no guaranteed order.
<script src="app.js" defer></script>
For most cases, defer is the right choice: it doesn't block rendering and keeps the order. async is for independent scripts, like analytics, that don't depend on anything else.
If you want to test a piece of logic — a regular expression, for example — without opening the console, the regex tester runs on the same JavaScript engine as the browser, so the behavior is identical to your code.
Frequently asked questions
Is JavaScript the same as Java?
No, and the similarity ends at the first four letters. Java is a compiled, strongly typed language created by Sun; JavaScript is dynamic and was born for the browser. The name was a marketing move by Netscape in 1995 to ride Java's popularity at the time.
Is JavaScript an interpreted or a compiled language?
Both, in practice. The engine interprets the code to start quickly and uses a JIT compiler to optimize the parts that run frequently, generating machine code at runtime. Calling it "purely interpreted" has been outdated for more than a decade.
Why do people say JavaScript is single-threaded if it does several things at once?
Because it has a single call stack — one operation at a time. The sense of parallelism comes from the event loop: slow tasks (network, timers) are delegated to the browser and their results come back through a queue when the stack is free. The language itself never executes two things simultaneously.
Do I need to learn plain JavaScript before a framework?
It's very much worth it. Frameworks like React and Svelte compile down to plain JavaScript, and when something goes wrong you debug at that level. People who understand the language underneath pick up any framework fast; people who skip that step tend to get stuck on the first non-standard error.
The language underneath is what counts
JavaScript is a dynamic, single-threaded language, executed by an engine that compiles your code on demand, coordinated by an event loop that gives the illusion of parallelism, and which talks to the page through the DOM — don't confuse the two. Understanding these layers is what separates people who copy stack traces from Google from people who read the error and know where to look. Frameworks come and go; the gears underneath have been the same for years, and that's what's worth investing in.
- 01 Does the PageSpeed Score Affect SEO? 100/100 and Failing Google doesn't rank on PageSpeed's 0 to 100 score but on Core Web Vitals from real users. Here's why a site scores 100/100 and still fails in the field.
- 02 Unit test vs integration test: the difference in practice The difference between unit and integration tests with API and database examples: what each one catches, what it costs, and when to use each in your project.