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.
B8101110002A00101010000000000000000000000000000000mov eax, 42Write 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.
Language rules
DOM, canvas, timers
Parsing and executing JavaScript, managing memory