# 5.2 Algorithm Conventions

The specification often uses a numbered list to specify steps in an algorithm. These algorithms are used to precisely specify the required semantics of ECMAScript language constructs. The algorithms are not intended to imply the use of any specific implementation technique. In practice, there may be more efficient algorithms available to implement a given feature.

Algorithms may be explicitly parameterized with an ordered, comma-separated sequence of alias names which may be used within the algorithm steps to reference the argument passed in that position. Optional parameters are denoted with surrounding brackets ([ , `name` ]) and are no different from required parameters within algorithm steps. A rest parameter may appear at the end of a parameter list, denoted with leading ellipsis (, ...`name`). The rest parameter captures all of the arguments provided following the required and optional parameters into a

Algorithm steps may be subdivided into sequential substeps. Substeps are indented and may themselves be further divided into indented substeps. Outline numbering conventions are used to identify substeps with the first level of substeps labelled with lower case alphabetic characters and the second level of substeps labelled with lower case roman numerals. If more than three levels are required these rules repeat with the fourth level using numeric labels. For example:

- Top-level step
- Substep.
- Substep.
- Subsubstep.
- Subsubsubstep
- Subsubsubsubstep
- Subsubsubsubsubstep

- Subsubsubsubstep

- Subsubsubstep

- Subsubstep.

A step or substep may be written as an “if” predicate that conditions its substeps. In this case, the substeps are only applied if the predicate is true. If a step or substep begins with the word “else”, it is a predicate that is the negation of the preceding “if” predicate step at the same level.

A step may specify the iterative application of its substeps.

A step that begins with “Assert:” asserts an invariant condition of its algorithm. Such assertions are used to make explicit algorithmic invariants that would otherwise be implicit. Such assertions add no additional semantic requirements and hence need not be checked by an implementation. They are used simply to clarify algorithms.

Algorithm steps may declare named aliases for any value using the form “Let `x` be `someValue`”. These aliases are reference-like in that both `x` and `someValue` refer to the same underlying data and modifications to either are visible to both. Algorithm steps that want to avoid this reference-like behaviour should explicitly make a copy of the right-hand side: “Let `x` be a copy of `someValue`” creates a shallow copy of `someValue`.

Once declared, an alias may be referenced in any subsequent steps and must not be referenced from steps prior to the alias's declaration. Aliases may be modified using the form “Set `x` to `someOtherValue`”.

# 5.2.1 Abstract Operations

In order to facilitate their use in multiple parts of this specification, some algorithms, called abstract operations, are named and written in parameterized functional form so that they may be referenced by name from within other algorithms. Abstract operations are typically referenced using a functional application style such as OperationName(`arg1`, `arg2`). Some abstract operations are treated as polymorphically dispatched methods of class-like specification abstractions. Such method-like abstract operations are typically referenced using a method application style such as `someValue`.OperationName(`arg1`, `arg2`).

# 5.2.2 Syntax-Directed Operations

A syntax-directed operation is a named operation whose definition consists of algorithms, each of which is associated with one or more productions from one of the ECMAScript grammars. A production that has multiple alternative definitions will typically have a distinct algorithm for each alternative. When an algorithm is associated with a grammar production, it may reference the terminal and nonterminal symbols of the production alternative as if they were parameters of the algorithm. When used in this manner, nonterminal symbols refer to the actual alternative definition that is matched when parsing the source text. The source text matched by a grammar production is the portion of the source text that starts at the beginning of the first terminal that participated in the match and ends at the end of the last terminal that participated in the match.

When an algorithm is associated with a production alternative, the alternative is typically shown without any “[ ]” grammar annotations. Such annotations should only affect the syntactic recognition of the alternative and have no effect on the associated semantics for the alternative.

Syntax-directed operations are invoked with a parse node and, optionally, other parameters by using the conventions on steps

- Let
`status`be SyntaxDirectedOperation ofSomeNonTerminal . - Let
`someParseNode`be the parse of some source text. - Perform SyntaxDirectedOperation of
`someParseNode`. - Perform SyntaxDirectedOperation of
`someParseNode`passing"value" as the argument.

Unless explicitly specified otherwise, all chain productions have an implicit definition for every operation that might be applied to that production's left-hand side nonterminal. The implicit definition simply reapplies the same operation with the same parameters, if any, to the

but the Evaluation operation does not associate an algorithm with that production. In that case, the Evaluation operation implicitly includes an association of the form:

**Runtime Semantics: Evaluation**

- Return the result of evaluating
StatementList .

# 5.2.3 Runtime Semantics

Algorithms which specify semantics that must be called at runtime are called runtime semantics. Runtime semantics are defined by

# 5.2.3.1 Implicit Completion Values

The algorithms of this specification often implicitly return

- Return
"Infinity" .

means the same thing as:

- Return
NormalCompletion ("Infinity" ).

However, if the value expression of a “return” statement is a

The abstract operation `completionRecord`) is used to emphasize that a previously computed `completionRecord`, and performs the following steps:

Assert :`completionRecord`is aCompletion Record .- Return
`completionRecord`as theCompletion Record of this abstract operation.

A “return” statement without a value in an algorithm step means the same thing as:

- Return
NormalCompletion (undefined ).

Any reference to a

# 5.2.3.2 Throw an Exception

Algorithms steps that say to throw an exception, such as

- Throw a
TypeError exception.

mean the same things as:

- Return
ThrowCompletion (a newly createdTypeError object).

# 5.2.3.3 ReturnIfAbrupt

Algorithms steps that say or are otherwise equivalent to:

ReturnIfAbrupt (`argument`).

mean the same thing as:

- If
`argument`is anabrupt completion , return`argument`. - Else if
`argument`is aCompletion Record , set`argument`to`argument`.[[Value]].

Algorithms steps that say or are otherwise equivalent to:

ReturnIfAbrupt (AbstractOperation()).

mean the same thing as:

- Let
`hygienicTemp`be AbstractOperation(). - If
`hygienicTemp`is anabrupt completion , return`hygienicTemp`. - Else if
`hygienicTemp`is aCompletion Record , set`hygienicTemp`to`hygienicTemp`.[[Value]].

Where `hygienicTemp` is ephemeral and visible only in the steps pertaining to ReturnIfAbrupt.

Algorithms steps that say or are otherwise equivalent to:

- Let
`result`be AbstractOperation(ReturnIfAbrupt (`argument`)).

mean the same thing as:

- If
`argument`is anabrupt completion , return`argument`. - If
`argument`is aCompletion Record , set`argument`to`argument`.[[Value]]. - Let
`result`be AbstractOperation(`argument`).

# 5.2.3.4 ReturnIfAbrupt Shorthands

Invocations of `?`

indicate that

- ? OperationName().

is equivalent to the following step:

ReturnIfAbrupt (OperationName()).

Similarly, for method application style, the step:

- ?
`someValue`.OperationName().

is equivalent to:

ReturnIfAbrupt (`someValue`.OperationName()).

Similarly, prefix `!`

is used to indicate that the following invocation of an abstract or syntax-directed operation will never return an

- Let
`val`be ! OperationName().

is equivalent to the following steps:

- Let
`val`be OperationName(). Assert :`val`is never anabrupt completion .- If
`val`is aCompletion Record , set`val`to`val`.[[Value]].

Syntax-directed operations for `!`

or `?`

before the invocation of the operation:

- Perform ! SyntaxDirectedOperation of
NonTerminal .

# 5.2.4 Static Semantics

Context-free grammars are not sufficiently powerful to express all the rules that define whether a stream of input elements form a valid ECMAScript

Static Semantic Rules have names and typically are defined using an algorithm. Named Static Semantic Rules are associated with grammar productions and a production that has multiple alternative definitions will typically have for each alternative a distinct algorithm for each applicable named static semantic rule.

A special kind of static semantic rule is an Early Error Rule.

# 5.2.5 Mathematical Operations

This specification makes reference to these kinds of numeric values:

*Mathematical values*: Arbitrary real numbers, used as the default numeric type.*Extended mathematical values*: Mathematical values together with +∞ and -∞.*Numbers*:IEEE 754-2019 double-precision floating point values.*BigInts*: ECMAScript values representing arbritary integers in a one-to-one correspondence.

In the language of this specification, numerical values are distinguished among different numeric kinds using subscript suffixes. The subscript _{𝔽} refers to Numbers, and the subscript _{ℤ} refers to BigInts. Numeric values without a subscript suffix refer to mathematical values.

Numeric operators such as +, ×, =, and ≥ refer to those operations as determined by the type of the operands. When applied to mathematical values, the operators refer to the usual mathematical operations. When applied to Numbers, the operators refer to the relevant operations within

In general, when this specification refers to a numerical value, such as in the phrase, "the length of `y`" or "the

Numeric operators applied to mixed-type operands (such as a Number and a

This specification denotes most numeric values in base 10; it also uses numeric values of the form 0x followed by digits 0-9 or A-F as base-16 values.

When the term integer is used in this specification, it refers to a

Conversions between mathematical values and Numbers or BigInts are always explicit in this document. A conversion from a `x` to a Number is denoted as "the `x`" or 𝔽(`x`), and is defined in `x` to a BigInt is denoted as "the BigInt value for `x`" or ℤ(`x`). A conversion from a Number or BigInt `x` to a `x`", or ℝ(`x`). The _{𝔽} and _{𝔽} is the `x` is the `x` for finite values, and is +∞ and -∞ for _{𝔽} and _{𝔽} respectively; it is not defined for

The mathematical function `x`)`x`, which is `x``x` < 0 and otherwise is `x` itself.

The mathematical function `x1`, `x2`, … , `xN`)`x1``xN``x1`, `x2`, ..., `xN`)`x1``xN`

The notation “`x` modulo `y``y` must be finite and non-zero) computes a value `k` of the same sign as `y` (or zero) such that `k`) < `y`) and `x` - `k` = `q` × `y``q`.

The phrase "the result of clamping `x` between `lower` and `upper`" (where `x` is an `lower` and `upper` are mathematical values such that `lower` ≤ `upper`) produces `lower` if `x` < `lower`, produces `upper` if `x` > `upper`, and otherwise produces `x`.

The mathematical function `x`)`x`.

Mathematical functions min, max,

# 5.2.6 Value Notation

In this specification, ECMAScript language values are displayed in `Function.prototype.apply`

or `let n = 42;`

.

Values which are internal to the specification and not directly observable from ECMAScript code are indicated with a