← Back to JavaScript, page 8

javascript-0151 · Easy fresher

What is JavaScript, and how does an engine such as V8 run your code?

JavaScript is a high-level, dynamically typed, multi-paradigm language standardized as ECMAScript. An engine such as V8 parses the source into a syntax tree, compiles it to bytecode that it starts running at once, and uses a just-in-time (JIT) compiler to turn hot code into optimized machine code. It runs on a single main thread inside a host environment, such as a browser or Node.js, that supplies APIs like the DOM, timers, and files.

Full answer

JavaScript is the programming language of the web. The language itself is defined by the ECMAScript specification, which is updated every year. The language has no built-in input or output. The host environment adds that: a browser provides the DOM, fetch, and timers, and Node.js provides files, networking, and process.

How code runs, in steps:

  1. Parsing: the engine reads the source text and checks the syntax. A syntax error stops the whole script before any line runs. The parser builds an abstract syntax tree (AST).
  2. Bytecode: an interpreter (Ignition in V8) turns the tree into compact bytecode and starts executing right away. This gives a fast start.
  3. Profiling and JIT: while the code runs, the engine records which functions are hot and what types they see. For hot code, an optimizing compiler (TurboFan in V8) produces machine code based on those assumptions.
  4. Deoptimization: if an assumption breaks (a function that always got numbers suddenly gets a string), the engine throws away the optimized code and falls back to bytecode.

Other facts to know: JavaScript is dynamically typed (variables hold any type, and types are checked while running), it is garbage collected, and it has one main thread with an event loop, so long synchronous work blocks everything else. Concurrency comes from asynchronous APIs and workers, not from threads that share objects.

Engines differ by environment: V8 (Chrome, Edge, Node.js, Deno), SpiderMonkey (Firefox), and JavaScriptCore (Safari, Bun). They follow the same specification. A JavaScript program is not purely interpreted and not purely compiled ahead of time: it is compiled while it runs.

Practical meaning: keep code simple and type-stable in hot paths, do not block the main thread, and measure instead of guessing about speed.

javascript
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const vm = require('node:vm');

console.log('engine:', 'V8', typeof process.versions.v8, '| host:', 'Node.js', typeof process.versions.node);

// 1. Parsing happens first: a syntax error means no line of the script runs
try {
  new vm.Script('console.log("this line never runs"); let = ;');
} catch (error) {
  console.log('whole script rejected at parse time:', error.name);
}

// 2. Dynamic typing: the same variable holds different types, checked while running
let value = 42;
console.log(typeof value);
value = 'now a string';
console.log(typeof value);

// 3. Bytecode: ask V8 to print the bytecode for a tiny function (run in a child process)
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bytecode-'));
const file = path.join(dir, 'add.js');
fs.writeFileSync(file, 'function add(a, b) { return a + b; }\nadd(1, 2);\n');
const dump = execFileSync(process.execPath, ['--print-bytecode', '--print-bytecode-filter=add', file], { encoding: 'utf8' });
const instructions = [...dump.matchAll(/@\s+\d+ : [0-9a-f ]+?\s{2,}(\w+)/g)].map((m) => m[1]);
console.log('bytecode of add(a, b):', instructions.join(' -> '));
fs.rmSync(dir, { recursive: true, force: true });

// 4. One main thread: a busy loop blocks timers, which only run after the synchronous code ends
const order = [];
setTimeout(() => order.push('timer (0 ms)'), 0);
const end = Date.now() + 30;
while (Date.now() < end) { /* blocking on purpose */ }
order.push('busy loop finished');
setTimeout(() => console.log(order), 10);

Run with Node.js v24.22.1 as js-151.js; two runs gave identical output. Output: engine: V8 string | host: Node.js string whole script rejected at parse time: SyntaxError number string bytecode of add(a, b): Ldar -> Add -> Return [ 'busy loop finished', 'timer (0 ms)' ]

Key points

  • ECMAScript is the standard; the host (browser or Node.js) provides APIs such as the DOM or files.
  • Engine pipeline: parse to AST, interpret bytecode, JIT-compile hot code, deoptimize if assumptions break.
  • JavaScript is dynamically typed, garbage collected, and single threaded with an event loop.
  • Syntax errors are found at parse time, before any code runs.
  • It is compiled at run time, not simply interpreted.

Common mistakes

  • Saying JavaScript is only interpreted, or only compiled in advance.
  • Mixing up the language (ECMAScript) with browser APIs such as the DOM.
  • Thinking that JavaScript is the same as Java.
  • Blocking the main thread with long synchronous work.

Follow-ups

  • What is the difference between the interpreter and the JIT compiler in an engine?
  • What is deoptimization and what can cause it?
  • Why do different engines behave the same for standard features?

Tips

  • Separate language, engine, and host environment in your first sentence.
  • Give the pipeline in four steps.
  • Mention that the main thread is single and what that means for slow code.