Reading Specification Algorithms

How to read the ECMAScript specification's numbered-step, abstract-operation style, including completion records and the ? abrupt-completion shorthand.

Purpose

The ECMAScript specification describes behavior as ordered algorithm steps operating on abstract operations — precisely defined procedures not directly callable from JavaScript, but describing exactly what an observable operation does underneath.

Reading the notation

ToPrimitive ( input [ , preferredType ] )
1. If input is an Object, then
   a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
   b. If exoticToPrim is not undefined, then
      i. Let result be ? Call(exoticToPrim, input, « hint »).
      ...
2. Return input.
  • Numbered/lettered steps run in order, top to bottom, exactly like reading pseudocode.
  • ? before a call means “if this returns an abrupt completion, propagate it immediately” — the formal model of an exception unwinding.
  • « » denotes a specification-level List (an ordered sequence), distinct from a JavaScript array.
  • @@toPrimitive refers to the well-known Symbol.toPrimitive.

Behavior notes

  • A completion record models the outcome of evaluating something: a normal completion (a value) or an abrupt completion (throw, return, break, continue) — the formal basis for control flow and exception propagation throughout the spec.
  • Abstract operations aren’t user-callable, but many correspond closely to something observable — ToPrimitive explains coercion, ToNumber explains numeric coercion, and so on.
  • Reading an algorithm doesn’t require reading the entire specification — following one operation’s steps, and the operations it in turn calls, is usually enough to answer a specific question.

Common mistakes

  • Assuming spec algorithms describe an engine’s actual internal implementation — they describe observable behavior precisely; a real engine can implement it any way that produces the same results, often very differently for performance.
  • Getting lost trying to read the entire specification linearly rather than following the specific operation relevant to the question at hand.

Runtime notes

A reading skill for the normative specification text — applies identically regardless of which engine’s behavior is being investigated, since all conformant engines implement the same algorithms.

Taught in these lessons