?
u
m
/
The source text of an ECMAScript
There are several situations where the identification of lexical input elements is sensitive to the syntactic grammar context that is consuming the input elements. This requires multiple
The use of multiple lexical goals ensures that there are no lexical ambiguities that would affect automatic semicolon insertion. For example, there are no syntactic grammar contexts where both a leading division or division-assignment, and a leading
a = b
/hi/g.exec(c).map(d);
where the first non-whitespace, non-comment code point after a
a = b / hi / g.exec(c).map(d);
The Unicode format-control characters (i.e., the characters in category “Cf” in the Unicode Character Database such as LEFT-TO-RIGHT MARK or RIGHT-TO-LEFT MARK) are control codes used to control the formatting of a range of text in the absence of higher-level protocols for this (such as mark-up languages).
It is useful to allow format-control characters in source text to facilitate editing and display. All format control characters may be used within comments, and within string literals, template literals, and regular expression literals.
U+200C (ZERO WIDTH NON-JOINER) and U+200D (ZERO WIDTH JOINER) are format-control characters that are used to make necessary distinctions when forming words or phrases in certain languages. In ECMAScript source text these code points may also be used in an
U+FEFF (ZERO WIDTH NO-BREAK SPACE) is a format-control character used primarily at the start of a text to mark it as Unicode and to allow detection of the text's encoding and byte order. <ZWNBSP> characters intended for this purpose can sometimes also appear after the start of a text, for example as a result of concatenating files. In ECMAScript source text <ZWNBSP> code points are treated as white space characters (see
The special treatment of certain format-control characters outside of comments, string literals, and regular expression literals is summarized in
Code Point | Name | Abbreviation | Usage |
---|---|---|---|
U+200C
|
ZERO WIDTH NON-JOINER | <ZWNJ> |
|
U+200D
|
ZERO WIDTH JOINER | <ZWJ> |
|
U+FEFF
|
ZERO WIDTH NO-BREAK SPACE | <ZWNBSP> |
|
White space code points are used to improve source text readability and to separate tokens (indivisible lexical units) from each other, but are otherwise insignificant. White space code points may occur between any two tokens and at the start or end of input. White space code points may occur within a
The ECMAScript white space code points are listed in
Code Point | Name | Abbreviation |
---|---|---|
U+0009
|
CHARACTER TABULATION | <TAB> |
U+000B
|
LINE TABULATION | <VT> |
U+000C
|
FORM FEED (FF) | <FF> |
U+FEFF
|
ZERO WIDTH NO-BREAK SPACE | <ZWNBSP> |
Category “Zs” | Any Unicode “Space_Separator” code point | <USP> |
U+0020 (SPACE) and U+00A0 (NO-BREAK SPACE) code points are part of <USP>.
Other than for the code points listed in
Like white space code points, line terminator code points are used to improve source text readability and to separate tokens (indivisible lexical units) from each other. However, unlike white space code points, line terminators have some influence over the behaviour of the syntactic grammar. In general, line terminators may occur between any two tokens, but there are a few places where they are forbidden by the syntactic grammar. Line terminators also affect the process of automatic semicolon insertion (
A line terminator can occur within a
Line terminators are included in the set of white space code points that are matched by the \s
class in regular expressions.
The ECMAScript line terminator code points are listed in
Code Point | Unicode Name | Abbreviation |
---|---|---|
U+000A
|
LINE FEED (LF) | <LF> |
U+000D
|
CARRIAGE RETURN (CR) | <CR> |
U+2028
|
LINE SEPARATOR | <LS> |
U+2029
|
PARAGRAPH SEPARATOR | <PS> |
Only the Unicode code points in
Comments can be either single or multi-line. Multi-line comments cannot nest.
Because a single-line comment can contain any Unicode code point except a //
marker to the end of the line. However, the
Comments behave like white space and are discarded except that, if a
A number of productions in this section are given alternative definitions in section
The
This standard specifies specific code point additions: U+0024 (DOLLAR SIGN) and U+005F (LOW LINE) are permitted anywhere in an
The definitions of the nonterminal
The nonterminal _
via
The sets of code points with Unicode properties “ID_Start” and “ID_Continue” include, respectively, the code points with Unicode properties “Other_ID_Start” and “Other_ID_Continue”.
Unicode escape sequences are permitted in an \
preceding the u
and { }
code units, if they appear, do not contribute code points to the \
Two
The syntax-directed operation IdentifierCodePoints takes no arguments and returns a
The syntax-directed operation IdentifierCodePoint takes no arguments and returns a code point. It is defined piecewise over the following productions:
A keyword is a token that matches fixed width
font, in some syntactic production. The keywords of ECMAScript include if
, while
, async
, await
, and many others.
A reserved word is an if
and while
are reserved words. await
is reserved only inside async functions and modules. async
is not reserved; it can be used as a variable name or statement label without restriction.
This specification uses a combination of grammatical productions and await
and yield
, are unconditionally reserved. Exceptions for await
and yield
are specified in
Those that are always allowed as identifiers, and are not keywords, such as Math
, window
, toString
, and _
;
Those that are never allowed as identifiers, namely the await
and yield
;
Those that are contextually allowed as identifiers, namely await
and yield
;
Those that are contextually disallowed as identifiers, in let
, static
, implements
, interface
, package
, private
, protected
, and public
;
Those that are always allowed as identifiers, but also appear as keywords within certain syntactic productions, at places where as
, async
, from
, get
, meta
, of
, set
, and target
.
The term conditional keyword, or contextual keyword, is sometimes used to refer to the keywords that fall in the last three categories, and thus can be used as identifiers in some contexts and as keywords in others.
Per \
An \
els\u{65}
. The
enum
is not currently used as a keyword in this specification. It is a future reserved word, set aside for use as a keyword in future language extensions.
Similarly, implements
, interface
, package
, private
, protected
, and public
are future reserved words in
The
For example: 3in
is an error and not the two input elements 3
and in
.
A numeric literal stands for a value of the Number type or the BigInt type.
The syntax-directed operation NumericValue takes no arguments and returns a Number or a BigInt. It is defined piecewise over the following productions:
A string literal is 0 or more Unicode code points enclosed in single or double quotes. Unicode code points may also be represented by an escape sequence. All code points may appear literally in a string literal except for the closing quote code points, U+005C (REVERSE SOLIDUS), U+000D (CARRIAGE RETURN), and U+000A (LINE FEED). Any code points may appear in the form of an escape sequence. String literals evaluate to ECMAScript String values. When generating these String values Unicode code points are UTF-16 encoded as defined in
The definition of the nonterminal
<LF> and <CR> cannot appear in a string literal, except as part of a \n
or \u000A
.
It is possible for string literals to precede a
function invalid() { "\7"; "use strict"; }
The syntax-directed operation SV takes no arguments and returns a String.
A string literal stands for a value of the String type. SV produces String values for string literals through recursive application on the various parts of the string literal. As part of this process, some Unicode code points within the string literal are interpreted as having a
Escape Sequence | Code Unit Value | Unicode Character Name | Symbol |
---|---|---|---|
\b
|
0x0008
|
BACKSPACE | <BS> |
\t
|
0x0009
|
CHARACTER TABULATION | <HT> |
\n
|
0x000A
|
LINE FEED (LF) | <LF> |
\v
|
0x000B
|
LINE TABULATION | <VT> |
\f
|
0x000C
|
FORM FEED (FF) | <FF> |
\r
|
0x000D
|
CARRIAGE RETURN (CR) | <CR> |
\"
|
0x0022
|
QUOTATION MARK |
"
|
\'
|
0x0027
|
APOSTROPHE |
'
|
\\
|
0x005C
|
REVERSE SOLIDUS |
\
|
A regular expression literal is an input element that is converted to a RegExp object (see ===
to each other even if the two literals' contents are identical. A RegExp object may also be created at runtime by new RegExp
or calling the RegExp
The productions below describe the syntax for a regular expression literal and are used by the input element scanner to find the end of the regular expression literal. The source text comprising the
An implementation may extend the ECMAScript Regular Expression grammar defined in
Regular expression literals may not be empty; instead of representing an empty regular expression literal, the code unit sequence //
starts a single-line comment. To specify an empty regular expression, use: /(?:)/
.
The syntax-directed operation BodyText takes no arguments and returns source text. It is defined piecewise over the following productions:
The syntax-directed operation FlagText takes no arguments and returns source text. It is defined piecewise over the following productions:
The syntax-directed operation TV takes no arguments and returns a String or
The syntax-directed operation TRV takes no arguments and returns a String. A template literal component is interpreted by TRV as a value of the String type. TRV is used to construct the raw components of a template object (colloquially, the template raw values). TRV is similar to
Most ECMAScript statements and declarations must be terminated with a semicolon. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.
In the following rules, “token” means the actual recognized lexical token determined using the current lexical
There are three basic rules of semicolon insertion:
When, as the source text is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:
}
.
)
and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement (However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a for
statement (see
The following are the only restricted productions in the grammar:
The practical effect of these restricted productions is as follows:
++
or --
token is encountered where the parser would treat it as a postfix operator, and at least one ++
or --
token, then a semicolon is automatically inserted before the ++
or --
token.
continue
, break
, return
, throw
, or yield
token is encountered and a continue
, break
, return
, throw
, or yield
token.
=>
token, a semicolon is automatically inserted and the punctuator causes a syntax error.
async
token is followed by a function
or (
token, a semicolon is automatically inserted and the async
token is not treated as part of the same expression or class element as the following tokens.
async
token is followed by a *
token, a semicolon is automatically inserted and the punctuator causes a syntax error.
The resulting practical advice to ECMAScript programmers is:
++
or --
operator should be on the same line as its operand.
return
or throw
statement or an yield
expression should start on the same line as the return
, throw
, or yield
token.
break
or continue
statement should be on the same line as the break
or continue
token.
=>
should be on the same line.
async
token preceding an asynchronous function or method should be on the same line as the immediately following token.
The source
{ 1 2 } 3
is not a valid sentence in the ECMAScript grammar, even with the automatic semicolon insertion rules. In contrast, the source
{ 1
2 } 3
is also not a valid ECMAScript sentence, but is transformed by automatic semicolon insertion into the following:
{ 1
;2 ;} 3;
which is a valid ECMAScript sentence.
The source
for (a; b
)
is not a valid ECMAScript sentence and is not altered by automatic semicolon insertion because the semicolon is needed for the header of a for
statement. Automatic semicolon insertion never inserts one of the two semicolons in the header of a for
statement.
The source
return
a + b
is transformed by automatic semicolon insertion into the following:
return;
a + b;
The expression a + b
is not treated as a value to be returned by the return
statement, because a return
.
The source
a = b
++c
is transformed by automatic semicolon insertion into the following:
a = b;
++c;
The token ++
is not treated as a postfix operator applying to the variable b
, because a b
and ++
.
The source
if (a > b)
else c = d
is not a valid ECMAScript sentence and is not altered by automatic semicolon insertion before the else
token, even though no production of the grammar applies at that point, because an automatically inserted semicolon would then be parsed as an empty statement.
The source
a = b + c
(d + e).print()
is not transformed by automatic semicolon insertion, because the parenthesized expression that begins the second line can be interpreted as an argument list for a function call:
a = b + c(d + e).print()
In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion.
ECMAScript programs can be written in a style with very few semicolons by relying on automatic semicolon insertion. As described above, semicolons are not inserted at every newline, and automatic semicolon insertion can depend on multiple tokens across line terminators.
As new syntactic features are added to ECMAScript, additional grammar productions could be added that cause lines relying on automatic semicolon insertion preceding them to change grammar productions when parsed.
For the purposes of this section, a case of automatic semicolon insertion is considered interesting if it is a place where a semicolon may or may not be inserted, depending on the source text which precedes it. The rest of this section describes a number of interesting cases of automatic semicolon insertion in this version of ECMAScript.
In a
(
). Without a semicolon, the two lines together are treated as a [
). Without a semicolon, the two lines together are treated as property access, rather than an `
). Without a semicolon, the two lines together are interpreted as a tagged Template (+
or -
. Without a semicolon, the two lines together are interpreted as a usage of the corresponding binary operator./
ECMAScript contains grammar productions which include “[no
The rest of this section describes a number of productions using “[no