# How JavaScript Runs

> Before you write a line, know what actually runs it: the engine, the host, and how scripts load.

Subject: JavaScript | Lesson 1 of 63 | 5 min read
Interactive lesson: https://codewithanimation.in/javascript/how-js-runs
Next: [Variables & Scope](https://codewithanimation.in/javascript/variables-and-scope.md)

## An engine turns your source into running code

JavaScript is a high-level language that runs inside an engine: `V8` in Chrome and Node, `SpiderMonkey` in Firefox, `JavaScriptCore` in Safari. You never run JS directly on the CPU; the engine does it for you.

The engine first parses your source text into a tree that represents its structure (an Abstract Syntax Tree). It then compiles that tree to bytecode and starts running it right away. This is why people say JS is `just-in-time compiled`, not purely interpreted.

While your code runs, the engine watches which functions run often (`hot` code) and recompiles those to optimized machine code for speed. If an assumption breaks, for example a variable that was always a number suddenly holds a string, it quietly falls back to the slower path.

## The language is small; the host gives it powers

The JavaScript language itself (the `ECMAScript` standard) defines syntax, types, functions, and built-ins like `Array` and `Math`. It does not know what a web page or a file is.

Those abilities come from the host environment. In a browser you get `document`, `fetch`, `setTimeout`, and `localStorage`. In Node you get `fs`, `process`, and `require`. Same language, different powers.

Knowing this split saves real confusion: `fetch` failing in an old Node version is not a JavaScript problem, it is a host that did not provide that API. The language and its surroundings are separate layers.

## Loading a script: default, defer, async, module

A plain `<script src="...">` pauses HTML parsing while it downloads and runs. Put many at the top of `<head>` and the page stalls before anything paints.

`<script defer>` downloads in the background and runs after the HTML is fully parsed, in document order. This is the safe default for scripts that touch the page.

`<script async>` downloads in the background and runs the instant it is ready, which may be before or after parsing finishes, and in no guaranteed order. Use it for independent scripts like analytics.

`<script type="module">` is deferred by default, runs in strict mode automatically, and gets its own module scope so top-level variables do not leak to the global object. This is the modern default.

## Common mistake: Assuming a Web API is part of the language

`setTimeout`, `fetch`, `alert`, and `document` are not JavaScript features; they are host APIs. Code that relies on them will throw in an environment that does not provide them (for example calling `document` in plain Node).

The reverse trap: assuming JS runs top-to-bottom exactly as written. The engine hoists declarations and defers module scripts, so execution order is not always source order. Later concepts (`hoisting`, the `event loop`) unpack this.

## Tip: Default to modules and defer

For new projects, load your entry script with `type="module"`. You get strict mode, real `import`/`export`, and deferred execution for free, with no leaking globals. Reach for `async` only for truly independent third-party scripts, and avoid plain blocking `<script>` in `<head>`.

## Try it: Try it

Follow one line of source as it moves through the engine, and see which layer owns each piece.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/how-js-runs

## Check yourself

### Question 1

Which of these is provided by the host environment, not the JavaScript language itself?

- A. `document.querySelector`
- B. `Array.prototype.map`
- C. `Math.max`
- D. The `typeof` operator

Correct answer: A

`document` is a browser (host) API, part of the DOM, not the JavaScript language. The language is ECMAScript; the host adds `document`, `fetch`, `setTimeout`, and more.

- Why not B: `Array` and its methods are defined by ECMAScript, the language standard. They exist in every JS environment.
- Why not C: `Math` is a built-in of the language. It is available everywhere JS runs.
- Why not D: `typeof` is a core language operator, part of ECMAScript syntax.

### Question 2

You want a script that touches the page to run after the HTML is parsed, in order. Which attribute?

- A. `defer`
- B. `async`
- C. No attribute (plain script)
- D. `charset`

Correct answer: A

`defer` downloads the script in parallel with parsing, then runs it after the document is fully parsed, in document order. It is the safe default for scripts that read or change the page.

- Why not B: `async` runs as soon as it downloads, which may be before parsing finishes and in unpredictable order.
- Why not C: A plain script blocks HTML parsing while it runs, which is exactly what you want to avoid.
- Why not D: `charset` declares the script's character encoding. It has nothing to do with execution timing.

### Question 3

What does a JIT (just-in-time) compiler do that a pure interpreter does not?

- A. Recompiles frequently-run code to optimized machine code while the program runs
- B. Runs the code once with no compilation at all
- C. Translates the whole program to machine code before running, like C
- D. Removes the need for a runtime engine

Correct answer: A

A JIT compiler starts your code quickly (via bytecode) and then, while the program runs, recompiles the hot functions to fast machine code, deoptimizing if its assumptions break. It blends the fast startup of an interpreter with the speed of compiled code.

- Why not B: That describes a pure interpreter. A JIT adds compilation of hot paths on top.
- Why not C: That is ahead-of-time compilation. A JIT compiles during execution, not fully before it.
- Why not D: The JIT is part of the engine's runtime; it does not replace it.

## Remember this

- An engine (V8, SpiderMonkey, JavaScriptCore) parses your source to an AST, then JIT-compiles and runs it.
- The language is ECMAScript; `document`, `fetch`, `setTimeout` come from the host (browser or Node), not the language.
- `defer` runs after parsing in order; `async` runs as soon as it loads; `type="module"` is deferred and strict by default.
- Execution is not always source order: hoisting and deferred scripts change when things run.
