5.1 Syntactic and Lexical Grammars
5.1.1 Context-Free Grammars
Acontext-free grammar consists of a number ofproductions. Each production has an abstract symbol called anonterminal as itsleft-hand side, and a sequence of zero or more nonterminal andterminal symbols as itsright-hand side. For each grammar, the terminal symbols are drawn from a specified alphabet.
Achain production is a production that has exactly one nonterminal symbol on its right-hand side along with zero or more terminal symbols.
Starting from a sentence consisting of a single distinguished nonterminal, called thegoal symbol, a given context-free grammar specifies alanguage, namely, the (perhaps infinite) set of possible sequences of terminal symbols that can result from repeatedly replacing any nonterminal in the sequence with a right-hand side of a production for which the nonterminal is the left-hand side.
5.1.2 The Lexical and RegExp Grammars
Alexical grammar for ECMAScript is given in clause12. This grammar has as its terminal symbols Unicode code points that conform to the rules forSourceCharacter defined in11.1. It defines a set of productions, starting from thegoal symbolInputElementDiv,InputElementTemplateTail,InputElementRegExp,InputElementRegExpOrTemplateTail, orInputElementHashbangOrRegExp, that describe how sequences of such code points are translated into a sequence of input elements.
Input elements other than white space and comments form the terminal symbols for the syntactic grammar for ECMAScript and are called ECMAScripttokens. These tokens are thereserved words, identifiers, literals, and punctuators of the ECMAScript language. Moreover, line terminators, although not considered to be tokens, also become part of the stream of input elements and guide the process of automatic semicolon insertion (12.10). Simple white space and single-line comments are discarded and do not appear in the stream of input elements for the syntactic grammar. AMultiLineComment (that is, a comment of the form/*
…*/
regardless of whether it spans more than one line) is likewise simply discarded if it contains no line terminator; but if aMultiLineComment contains one or more line terminators, then it is replaced by a single line terminator, which becomes part of the stream of input elements for the syntactic grammar.
ARegExp grammar for ECMAScript is given in22.2.1. This grammar also has as its terminal symbols the code points as defined bySourceCharacter. It defines a set of productions, starting from thegoal symbolPattern, that describe how sequences of code points are translated into regular expression patterns.
Productions of the lexical and RegExp grammars are distinguished by having two colons “::” as separating punctuation. The lexical and RegExp grammars share some productions.
5.1.3 The Numeric String Grammar
Anumeric string grammar appears in7.1.4.1. It has as its terminal symbolsSourceCharacter, and is used for translating Strings into numeric values starting from thegoal symbolStringNumericLiteral (which is similar to but distinct from thelexical grammar for numeric literals).
Productions of the numeric string grammar are distinguished by having three colons “:::” as punctuation, and are never used for parsing source text.
5.1.4 The Syntactic Grammar
Thesyntactic grammar for ECMAScript is given in clauses13 through16. This grammar has ECMAScript tokens defined by the lexical grammar as its terminal symbols (5.1.2). It defines a set of productions, starting from two alternativegoal symbolsScript andModule, that describe how sequences of tokens form syntactically correct independent components of ECMAScript programs.
When a stream of code points is to be parsed as an ECMAScriptScript orModule, it is first converted to a stream of input elements by repeated application of the lexical grammar; this stream of input elements is then parsed by a single application of the syntactic grammar. The input stream is syntactically in error if the tokens in the stream of input elements cannot be parsed as a single instance of the goal nonterminal (Script orModule), with no tokens left over.
When a parse is successful, it constructs aparse tree, a rooted tree structure in which each node is aParse Node. Each Parse Node is aninstance of a symbol in the grammar; it represents a span of the source text that can be derived from that symbol. The root node of the parse tree, representing the whole of the source text, is an instance of the parse'sgoal symbol. When a Parse Node is an instance of a nonterminal, it is also an instance of some production that has that nonterminal as its left-hand side. Moreover, it has zero or morechildren, one for each symbol on the production's right-hand side: each child is a Parse Node that is an instance of the corresponding symbol.
New Parse Nodes are instantiated for each invocation of the parser and never reused between parses even of identical source text. Parse Nodes are consideredthe same Parse Node if and only if they represent the same span of source text, are instances of the same grammar symbol, and resulted from the same parser invocation.
Note 1Parsing the same String multiple times will lead to different Parse Nodes. For example, consider:
let str ="1 + 1;";eval(str);eval(str);
Each call toeval
converts the value ofstr
intoECMAScript source text and performs an independent parse that creates its own separate tree of Parse Nodes. The trees are distinct even though each parse operates upon a source text that was derived from the same String value.
Note 2Parse Nodes are specification artefacts, and implementations are not required to use an analogous data structure.
Productions of the syntactic grammar are distinguished by having just one colon “:” as punctuation.
The syntactic grammar as presented in clauses13 through16 is not a complete account of which token sequences are accepted as a correct ECMAScriptScript orModule. Certain additional token sequences are also accepted, namely, those that would be described by the grammar if only semicolons were added to the sequence in certain places (such as before line terminator characters). Furthermore, certain token sequences that are described by the grammar are not considered acceptable if a line terminator character appears in certain “awkward” places.
In certain cases, in order to avoid ambiguities, the syntactic grammar uses generalized productions that permit token sequences that do not form a valid ECMAScriptScript orModule. For example, this technique is used for object literals and object destructuring patterns. In such cases a more restrictivesupplemental grammar is provided that further restricts the acceptable token sequences. Typically, anearly error rule will then state that, in certain contexts, "Pmust cover anN", whereP is a Parse Node (an instance of the generalized production) andN is a nonterminal from the supplemental grammar. This means:
- The sequence of tokens originally matched byP is parsed again usingN as thegoal symbol. IfN takes grammatical parameters, then they are set to the same values used whenP was originally parsed.
- If the sequence of tokens can be parsed as a single instance ofN, with no tokens left over, then:
- We refer to that instance ofN (a Parse Node, unique for a givenP) as "theN that iscovered byP".
- All Early Error rules forN and its derived productions also apply to theN that is covered byP.
- Otherwise (if the parse fails), it is an early Syntax Error.
5.1.5 Grammar Notation
5.1.5.1 Terminal Symbols
In the ECMAScript grammars, some terminal symbols are shown infixed-width
font. These are to appear in a source text exactly as written. All terminal symbol code points specified in this way are to be understood as the appropriate Unicode code points from the Basic Latin block, as opposed to any similar-looking code points from other Unicode ranges. A code point in a terminal symbol cannot be expressed by a\
UnicodeEscapeSequence.
In grammars whose terminal symbols are individual Unicode code points (i.e., the lexical, RegExp, and numeric string grammars), a contiguous run of multiple fixed-width code points appearing in a production is a simple shorthand for the same sequence of code points, written as standalone terminal symbols.
For example, the production:
HexIntegerLiteral::0xHexDigitsis a shorthand for:
HexIntegerLiteral::0xHexDigitsIn contrast, in the syntactic grammar, a contiguous run of fixed-width code points is a single terminal symbol.
Terminal symbols come in two other forms:
- In the lexical and RegExp grammars, Unicode code points without a conventional printed representation are instead shown in the form "<ABBREV>" where "ABBREV" is a mnemonic for the code point or set of code points. These forms are defined inUnicode Format-Control Characters,White Space, andLine Terminators.
- In the syntactic grammar, certain terminal symbols (e.g.IdentifierName andRegularExpressionLiteral) are shown in italics, as they refer to the nonterminals of the same name in the lexical grammar.
5.1.5.2 Nonterminal Symbols and Productions
Nonterminal symbols are shown initalic type. The definition of a nonterminal (also called a “production”) is introduced by the name of the nonterminal being defined followed by one or more colons. (The number of colons indicates to which grammar the production belongs.) One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the syntactic definition:
WhileStatement:while(Expression)Statementstates that the nonterminalWhileStatement represents the tokenwhile
, followed by a left parenthesis token, followed by anExpression, followed by a right parenthesis token, followed by aStatement. The occurrences ofExpression andStatement are themselves nonterminals. As another example, the syntactic definition:
ArgumentList:AssignmentExpressionArgumentList,AssignmentExpressionstates that anArgumentList may represent either a singleAssignmentExpression or anArgumentList, followed by a comma, followed by anAssignmentExpression. This definition ofArgumentList is recursive, that is, it is defined in terms of itself. The result is that anArgumentList may contain any positive number of arguments, separated by commas, where each argument expression is anAssignmentExpression. Such recursive definitions of nonterminals are common.
5.1.5.3 Optional Symbols
The subscripted suffix “opt”, which may appear after a terminal or nonterminal, indicates an optional symbol. The alternative containing the optional symbol actually specifies two right-hand sides, one that omits the optional element and one that includes it. This means that:
VariableDeclaration:BindingIdentifierInitializeroptis a convenient abbreviation for:
VariableDeclaration:BindingIdentifierBindingIdentifierInitializerand that:
ForStatement:for(LexicalDeclarationExpressionopt;Expressionopt)Statementis a convenient abbreviation for:
ForStatement:for(LexicalDeclaration;Expressionopt)Statementfor(LexicalDeclarationExpression;Expressionopt)Statementwhich in turn is an abbreviation for:
ForStatement:for(LexicalDeclaration;)Statementfor(LexicalDeclaration;Expression)Statementfor(LexicalDeclarationExpression;)Statementfor(LexicalDeclarationExpression;Expression)Statementso, in this example, the nonterminalForStatement actually has four alternative right-hand sides.
5.1.5.4 Grammatical Parameters
A production may be parameterized by a subscripted annotation of the form “[parameters]”, which may appear as a suffix to the nonterminal symbol defined by the production. “parameters” may be either a single name or a comma separated list of names. A parameterized production is shorthand for a set of productions defining all combinations of the parameter names, preceded by an underscore, appended to the parameterized nonterminal symbol. This means that:
StatementList[Return]:ReturnStatementExpressionStatementis a convenient abbreviation for:
StatementList:ReturnStatementExpressionStatementStatementList_Return:ReturnStatementExpressionStatementand that:
StatementList[Return, In]:ReturnStatementExpressionStatementis an abbreviation for:
StatementList:ReturnStatementExpressionStatementStatementList_Return:ReturnStatementExpressionStatementStatementList_In:ReturnStatementExpressionStatementStatementList_Return_In:ReturnStatementExpressionStatementMultiple parameters produce a combinatoric number of productions, not all of which are necessarily referenced in a complete grammar.
References to nonterminals on the right-hand side of a production can also be parameterized. For example:
StatementList:ReturnStatementExpressionStatement[+In]is equivalent to saying:
StatementList:ReturnStatementExpressionStatement_Inand:
StatementList:ReturnStatementExpressionStatement[~In]is equivalent to:
StatementList:ReturnStatementExpressionStatementA nonterminal reference may have both a parameter list and an “opt” suffix. For example:
VariableDeclaration:BindingIdentifierInitializer[+In]optis an abbreviation for:
VariableDeclaration:BindingIdentifierBindingIdentifierInitializer_InPrefixing a parameter name with “?” on a right-hand side nonterminal reference makes that parameter value dependent upon the occurrence of the parameter name on the reference to the current production's left-hand side symbol. For example:
VariableDeclaration[In]:BindingIdentifierInitializer[?In]is an abbreviation for:
VariableDeclaration:BindingIdentifierInitializerVariableDeclaration_In:BindingIdentifierInitializer_InIf a right-hand side alternative is prefixed with “[+parameter]” that alternative is only available if the named parameter was used in referencing the production's nonterminal symbol. If a right-hand side alternative is prefixed with “[~parameter]” that alternative is only available if the named parameter wasnot used in referencing the production's nonterminal symbol. This means that:
StatementList[Return]:[+Return]ReturnStatementExpressionStatementis an abbreviation for:
StatementList:ExpressionStatementStatementList_Return:ReturnStatementExpressionStatementand that:
StatementList[Return]:[~Return]ReturnStatementExpressionStatementis an abbreviation for:
StatementList:ReturnStatementExpressionStatementStatementList_Return:ExpressionStatement5.1.5.5 one of
When the words “one of” follow the colon(s) in a grammar definition, they signify that each of the terminal symbols on the following line or lines is an alternative definition. For example, the lexical grammar for ECMAScript contains the production:
NonZeroDigit::one of123456789which is merely a convenient abbreviation for:
NonZeroDigit::1234567895.1.5.6 [empty]
If the phrase “[empty]” appears as the right-hand side of a production, it indicates that the production's right-hand side contains no terminals or nonterminals.
5.1.5.7 Lookahead Restrictions
If the phrase “[lookahead =seq]” appears in the right-hand side of a production, it indicates that the production may only be used if the token sequenceseq is a prefix of the immediately following input token sequence. Similarly, “[lookahead ∈set]”, whereset is afinite non-empty set of token sequences, indicates that the production may only be used if some element ofset is a prefix of the immediately following token sequence. For convenience, the set can also be written as a nonterminal, in which case it represents the set of all token sequences to which that nonterminal could expand. It is considered an editorial error if the nonterminal could expand to infinitely many distinct token sequences.
These conditions may be negated. “[lookahead ≠seq]” indicates that the containing production may only be used ifseq isnot a prefix of the immediately following input token sequence, and “[lookahead ∉set]” indicates that the production may only be used ifno element ofset is a prefix of the immediately following token sequence.
As an example, given the definitions:
DecimalDigit::one of0123456789DecimalDigits::DecimalDigitDecimalDigitsDecimalDigitthe definition:
LookaheadExample::n[lookahead ∉ {1,3,5,7,9 }]DecimalDigitsDecimalDigit[lookahead ∉DecimalDigit]matches either the lettern
followed by one or more decimal digits the first of which is even, or a decimal digit not followed by another decimal digit.
Note that when these phrases are used in the syntactic grammar, it may not be possible to unambiguously identify the immediately following token sequence because determining later tokens requires knowing which lexicalgoal symbol to use at later positions. As such, when these are used in the syntactic grammar, it is considered an editorial error for a token sequenceseq to appear in a lookahead restriction (including as part of a set of sequences) if the choices of lexicalgoal symbols to use could change whether or notseq would be a prefix of the resulting token sequence.
If the phrase “[noLineTerminator here]” appears in the right-hand side of a production of the syntactic grammar, it indicates that the production isa restricted production: it may not be used if aLineTerminator occurs in the input stream at the indicated position. For example, the production:
ThrowStatement:throw[noLineTerminator here]Expression;indicates that the production may not be used if aLineTerminator occurs in the script between thethrow
token and theExpression.
Unless the presence of aLineTerminator is forbidden by a restricted production, any number of occurrences ofLineTerminator may appear between any two consecutive tokens in the stream of input elements without affecting the syntactic acceptability of the script.
5.1.5.9 but not
The right-hand side of a production may specify that certain expansions are not permitted by using the phrase “but not” and then indicating the expansions to be excluded. For example, the production:
Identifier::IdentifierNamebut notReservedWordmeans that the nonterminalIdentifier may be replaced by any sequence of code points that could replaceIdentifierName provided that the same sequence of code points could not replaceReservedWord.
5.1.5.10 Descriptive Phrases
Finally, a few nonterminal symbols are described by a descriptive phrase in sans-serif type in cases where it would be impractical to list all the alternatives:
SourceCharacter::any Unicode code point5.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 aList. If there are no such additional arguments, thatList is empty.
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 lowercase alphabetic characters and the second level of substeps labelled with lowercase 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
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 “Letx besomeValue”. These aliases are reference-like in that bothx andsomeValue 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: “Letx be a copy ofsomeValue” creates a shallow copy ofsomeValue.
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 “Setx tosomeOtherValue”.
5.2.1 Abstract Operations
In order to facilitate their use in multiple parts of this specification, some algorithms, calledabstract 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 assomeValue.OperationName(arg1,arg2).
5.2.2 Syntax-Directed Operations
Asyntax-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. Thesource text matched by a grammar production orParse Node derived from it 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 steps1,3, and4 in the following algorithm:
- Letstatus be SyntaxDirectedOperation ofSomeNonTerminal.
- LetsomeParseNode be the parse of some source text.
- Perform SyntaxDirectedOperation ofsomeParseNode.
- Perform SyntaxDirectedOperation ofsomeParseNode with argument"value".
Unless explicitly specified otherwise, allchain 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 thechain production's sole right-hand side nonterminal and then returns the result. For example, assume that some algorithm has a step of the form: “ReturnEvaluation ofBlock” and that there is a production:
Block:{StatementList}but theEvaluation operation does not associate an algorithm with that production. In that case, theEvaluation operation implicitly includes an association of the form:
Runtime Semantics:Evaluation
Block:{StatementList}- ReturnEvaluation ofStatementList.
5.2.3 Runtime Semantics
Algorithms which specify semantics that must be called at runtime are calledruntime semantics. Runtime semantics are defined byabstract operations orsyntax-directed operations.
5.2.3.1 Completion (completionRecord )
The abstract operation Completion takes argumentcompletionRecord (aCompletion Record) and returns aCompletion Record. It is used to emphasize that aCompletion Record is being returned. It performs the following steps when called:
- Assert:completionRecord is aCompletion Record.
- ReturncompletionRecord.
5.2.3.2 Throw an Exception
Algorithms steps that say to throw an exception, such as
- Throw aTypeError exception.
mean the same things as:
- ReturnThrowCompletion(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:
- Assert:argument is aCompletion Record.
- Ifargument is anabrupt completion, returnCompletion(argument).
- Else, setargument toargument.[[Value]].
Algorithms steps that say or are otherwise equivalent to:
- ReturnIfAbrupt(AbstractOperation()).
mean the same thing as:
- LethygienicTemp be AbstractOperation().
- Assert:hygienicTemp is aCompletion Record.
- IfhygienicTemp is anabrupt completion, returnCompletion(hygienicTemp).
- Else, sethygienicTemp tohygienicTemp.[[Value]].
WherehygienicTemp is ephemeral and visible only in the steps pertaining to ReturnIfAbrupt.
Algorithms steps that say or are otherwise equivalent to:
- Letresult be AbstractOperation(ReturnIfAbrupt(argument)).
mean the same thing as:
- Assert:argument is aCompletion Record.
- Ifargument is anabrupt completion, returnCompletion(argument).
- Else, setargument toargument.[[Value]].
- Letresult be AbstractOperation(argument).
5.2.3.4 ReturnIfAbrupt Shorthands
Invocations ofabstract operations andsyntax-directed operations that are prefixed by?
indicate thatReturnIfAbrupt should be applied to the resultingCompletion Record. For example, the step:
- ? 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 orsyntax-directed operation will never return anabrupt completion and that the resultingCompletion Record's[[Value]] field should be used in place of the return value of the operation. For example, the step:
- Letval be ! OperationName().
is equivalent to the following steps:
- Letval be OperationName().
- Assert:val is anormal completion.
- Setval toval.[[Value]].
Syntax-directed operations forruntime semantics make use of this shorthand by placing!
or?
before the invocation of the operation:
- Perform ! SyntaxDirectedOperation ofNonTerminal.
5.2.3.5 Implicit Normal Completion
In algorithms withinabstract operations which are declared to return aCompletion Record, and within all built-in functions, the returned value is first passed toNormalCompletion, and the result is used instead. This rule does not apply within theCompletion algorithm or when the value being returned is clearly marked as aCompletion Record in that step; these cases are:
It is an editorial error if aCompletion Record is returned from such an abstract operation through any other means. For example, within theseabstract operations,
- Returntrue.
means the same things as any of
- ReturnNormalCompletion(true).
or
- Letcompletion beNormalCompletion(true).
- ReturnCompletion(completion).
or
- ReturnCompletion Record {[[Type]]:normal,[[Value]]:true,[[Target]]:empty }.
Note that, through theReturnIfAbrupt expansion, the following example is allowed, as within the expanded steps, the result of applyingCompletion is returned directly in the abrupt case and the implicitNormalCompletion application occurs after unwrapping in the normal case.
- Return ? completion.
The following example would be an editorial error because aCompletion Record is being returned without being annotated in that step.
- Letcompletion beNormalCompletion(true).
- Returncompletion.
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 ECMAScriptScript orModule that may be evaluated. In some situations additional rules are needed that may be expressed using either ECMAScript algorithm conventions or prose requirements. Such rules are always associated with a production of a grammar and are called thestatic semantics of the production.
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 anEarly Error Rule.Early error rules defineearly error conditions (see clause17) that are associated with specific grammar productions.Evaluation of mostearly error rules are not explicitly invoked within the algorithms of this specification. A conforming implementation must, prior to the first evaluation of aScript orModule, validate all of theearly error rules of the productions used to parse thatScript orModule. If any of theearly error rules are violated theScript orModule is invalid and cannot be evaluated.
5.2.5 Mathematical Operations
This specification makes reference to these kinds of numeric values:
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 tomathematical values. 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.
In general, when this specification refers to a numerical value, such as in the phrase, "the length ofy" or "theinteger represented by the four hexadecimal digits ...", without explicitly specifying a numeric kind, the phrase refers to amathematical value. Phrases which refer to a Number or a BigInt value are explicitly annotated as such; for example, "theNumber value for the number of code points in …" or "theBigInt value for …".
When the terminteger is used in this specification, it refers to amathematical value which is in the set ofintegers, unless otherwise stated. When the termintegral Number is used in this specification, it refers to afinite Number value whosemathematical value is in the set ofintegers.
Numeric operators such as +, ×, =, and ≥ refer to those operations as determined by the type of the operands. When applied tomathematical values, the operators refer to the usual mathematical operations. When applied toextended mathematical values, the operators refer to the usual mathematical operations over the extended real numbers; indeterminate forms are not defined and their use in this specification should be considered an editorial error. When applied to Numbers, the operators refer to the relevant operations withinIEEE 754-2019. When applied to BigInts, the operators refer to the usual mathematical operations applied to themathematical value of the BigInt. Numeric operators applied to mixed-type operands (such as a Number and amathematical value) are not defined and should be considered an editorial error in this specification.
Conversions betweenmathematical values and Numbers or BigInts are always explicit in this document. A conversion from amathematical value orextended mathematical valuex to a Number is denoted as "theNumber value forx" or𝔽(x), and is defined in6.1.6.1. A conversion from anintegerx to a BigInt is denoted as "theBigInt value forx" orℤ(x). A conversion from a Number or BigIntx to amathematical value is denoted as "themathematical value ofx", orℝ(x). Themathematical value of+0𝔽 and-0𝔽 is themathematical value 0. Themathematical value of non-finite values is not defined. Theextended mathematical value ofx is themathematical value ofx forfinite values, and is +∞ and -∞ for+∞𝔽 and-∞𝔽 respectively; it is not defined forNaN.
The mathematical functionabs(x) produces the absolute value ofx, which is-x ifx < 0 and otherwise isx itself.
The mathematical functionmin(x1,x2, … ,xN) produces the mathematically smallest ofx1 throughxN. The mathematical functionmax(x1,x2, ...,xN) produces the mathematically largest ofx1 throughxN. The domain and range of these mathematical functions are theextended mathematical values.
The notation “x moduloy” (y must befinite and non-zero) computes a valuek of the same sign asy (or zero) such thatabs(k) <abs(y) andx -k =q ×y for someintegerq.
The phrase "the result ofclampingx betweenlower andupper" (wherex is anextended mathematical value andlower andupper aremathematical values such thatlower ≤upper) produceslower ifx <lower, producesupper ifx >upper, and otherwise producesx.
The mathematical functionfloor(x) produces the largestinteger (closest to +∞) that is not larger thanx.
NoteThe mathematical functiontruncate(x) removes the fractional part ofx by rounding towards zero, producing-floor(-x) ifx < 0 and otherwise producingfloor(x).
Mathematical functionsmin,max,abs,floor, andtruncate are not defined for Numbers and BigInts, and any usage of those methods that have non-mathematical value arguments would be an editorial error in this specification.
Aninterval from lower bounda to upper boundb is a possibly-infinite, possibly-empty set of numeric values of the same numeric type. Each bound will be described as either inclusive or exclusive, but not both. There are four kinds of intervals, as follows:
- Aninterval froma (inclusive) tob (inclusive), also called aninclusive interval froma tob, includes all valuesx of the same numeric type such thata ≤x ≤b, and no others.
- Aninterval froma (inclusive) tob (exclusive) includes all valuesx of the same numeric type such thata ≤x <b, and no others.
- Aninterval froma (exclusive) tob (inclusive) includes all valuesx of the same numeric type such thata <x ≤b, and no others.
- Aninterval froma (exclusive) tob (exclusive) includes all valuesx of the same numeric type such thata <x <b, and no others.
For example, theinterval from 1 (inclusive) to 2 (exclusive) consists of allmathematical values between 1 and 2, including 1 and not including 2. For the purpose of defining intervals,-0𝔽 <+0𝔽, so, for example, aninclusive interval with a lower bound of+0𝔽 includes+0𝔽 but not-0𝔽.NaN is never included in aninterval.
5.2.6 Value Notation
In this specification,ECMAScript language values are displayed inbold. Examples includenull,true, or"hello". These are distinguished fromECMAScript source text such asFunction.prototype.apply
orlet n = 42;
.
5.2.7 Identity
In this specification, both specification values andECMAScript language values are compared for equality. When comparing for equality, values fall into one of two categories.Values without identity are equal to other values without identity if all of their innate characteristics are the same — characteristics such as the magnitude of aninteger or the length of a sequence. Values without identity may be manifest without prior reference by fully describing their characteristics. In contrast, eachvalue with identity is unique and therefore only equal to itself. Values with identity are like values without identity but with an additional unguessable, unchangeable, universally-unique characteristic calledidentity. References to existing values with identity cannot be manifest simply by describing them, as the identity itself is indescribable; instead, references to these values must be explicitly passed from one place to another. Some values with identity are mutable and therefore can have their characteristics (except their identity) changed in-place, causing all holders of the value to observe the new characteristics. A value without identity is never equal to a value with identity.
From the perspective of this specification, the word “is” is used to compare two values for equality, as in “Ifbool istrue, then ...”, and the word “contains” is used to search for a value inside lists using equality comparisons, as in "Iflist contains aRecordr such thatr.[[Foo]] istrue, then ...". Thespecification identity of values determines the result of these comparisons and is axiomatic in this specification.
From the perspective of the ECMAScript language, language values are compared for equality using theSameValue abstract operation and theabstract operations it transitively calls. The algorithms of these comparisonabstract operations determinelanguage identity ofECMAScript language values.
For specification values, examples of values without specification identity include, but are not limited to:mathematical values andextended mathematical values;ECMAScript source text,surrogate pairs,Directive Prologues, etc; UTF-16 code units; Unicode code points;enums;abstract operations, includingsyntax-directed operations,host hooks, etc; and ordered pairs. Examples of specification values with specification identity include, but are not limited to: any kind ofRecords, includingProperty Descriptors,PrivateElements, etc;Parse Nodes;Lists;Sets andRelations;Abstract Closures;Data Blocks;Private Names;execution contexts andexecution context stacks;agent signifiers; andWaiterList Records.
Specification identity agrees with language identity for allECMAScript language values except Symbol values produced bySymbol.for. TheECMAScript language values without specification identity and without language identity areundefined,null,Booleans,Strings,Numbers, andBigInts. TheECMAScript language values with specification identity and language identity areSymbols not produced bySymbol.for andObjects. Symbol values produced bySymbol.for have specification identity, but not language identity.