ECMAScript · Engine · Runtime01 / 19

Language, engine, and runtime

How JavaScript rules, a language implementation, and the program's environment differ.

JavaScript is a programming language. To understand how a program runs, we will distinguish three concepts: the language rules, the engine, and the runtime.

ECMAScript: language rules

The core rules of JavaScript are defined in the ECMAScript specification, ECMA-262. It defines the language’s syntax and behavior: how variables are declared, functions are called, and objects and operators work.

The specification describes what a program must do. It does not require a particular interpreter or compiler.

Engine: implementing the language

A JavaScript engine implements these rules: it prepares and executes code and manages objects and memory. The CPU executes machine instructions; the engine organizes JavaScript execution through interpretation or compilation.

Example · x86-64 · 5 bytesOne machine instruction
Bytes in hexadecimal notationThe same bytes in binary below
B810111000
2A00101010
0000000000
0000000000
0000000000
Readable notation · assemblymov eax, 42

Write the number 42 to the CPU's EAX register.

An actual encoding of the MOV instruction, chosen for this example. These bytes were not produced by compiling JavaScript in V8. Intel · MOV · volume 2B, page 4-35.

V8 is one such engine. JavaScriptCore and SpiderMonkey are other implementations of the same language.

Runtime: the execution environment

Runtime is the environment that embeds the engine. It starts the program and provides additional APIs.

The browser provides DOM, canvas, and timers. Node.js is another runtime. It also uses V8 but provides its own APIs, including APIs for working with files.

const player = { hp: 100 }; // Language values and objects
setTimeout(() => player.hp -= 10, 100); // Host timer API

Creating an object and changing hp follow the language rules. setTimeout is provided by the runtime. When the callback is invoked, the engine executes its JavaScript code.

ECMAScript

Language rules

Browser

DOM, canvas, timers

V8

Parsing and executing JavaScript, managing memory

V8 implements the ECMAScript rules. The browser embeds the engine and provides external APIs.
Key takeaway
  1. ECMAScript is the language standard: its syntax and behavior are defined in the ECMA-262 specification.
  2. An engine implements the language rules and executes JavaScript. V8 is one implementation.
  3. A runtime embeds the engine and provides external APIs to the program.

Check the primary source ↗

ECMA-262 · Hosts and Implementations↗Distinguishes the language, its implementation, and the host environment.V8 · Engine internals↗Documentation for the V8 implementation, rather than requirements for every JavaScript engine.HTML Standard · Timers↗The browser environment provides the setTimeout and setInterval timers.