7   Lexical Conventions

The source text of an ECMAScript program is first converted into a sequence of input elements, which are tokens, line terminators, comments, or white space. The source text is scanned from left to right, repeatedly taking the longest possible sequence of characters as the next input element.

There are two goal symbols for the lexical grammar. The InputElementDiv symbol is used in those syntactic grammar contexts where a leading division (/) or division-assignment (/=) operator is permitted. The InputElementRegExp symbol is used in other syntactic grammar contexts.

NOTE There are no syntactic grammar contexts where both a leading division or division-assignment, and a leading RegularExpressionLiteral are permitted. This is not affected by semicolon insertion (see 7.9); in examples such as the following:

a = b
/ hi / g.exec(c).map(d);

where the first non-whitespace, non-comment character after a LineTerminator is slash (/) and the syntactic context allows division or division-assignment, no semicolon is inserted at the LineTerminator. That is, the above example is interpreted in the same way as:

a = b / hi / g.exec(c).map(d);

(scroll to the next page)

Syntax
InputElementDiv ::
WhiteSpace
LineTerminator
Comment
Token
DivPunctuator
InputElementRegExp ::
WhiteSpace
LineTerminator
Comment
Token
RegularExpressionLiteral

7.1   Unicode Format-Control Characters

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 and regular expression literals.

<ZWNJ> and <ZWJ> are format-control characters that are used to make necessary distinctions when forming words or phrases in certain languages. In ECMAScript source text, <ZWNJ> and <ZWJ> may also be used in an identifier after the first character.

<BOM> 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. <BOM> characters intended for this purpose can sometimes also appear after the start of a text, for example as a result of concatenating files. <BOM> characters are treated as white space characters (see 7.2 ).

The special treatment of certain format-control characters outside of comments, string literals, and regular expression literals is summarized in Table 1.

Table 1 — Format-Control Character Usage
Code Unit ValueNameFormal NameUsage
  \u200CZero width non-joiner<ZWNJ>IdentifierPart
  \u200CZero width joiner<ZWJ>IdentifierPart
  \uFEFFByte Order Mark<BOM>Whitespace

7.2   White Space

White space characters are used to improve source text readability and to separate tokens (indivisible lexical units) from each other, but are otherwise insignificant. White space characters may occur between any two tokens and at the start or end of input. White space characters may also occur within a StringLiteral or a RegularExpressionLiteral (where they are considered significant characters forming part of the literal value) or within a Comment, but cannot appear within any other kind of token.

The ECMAScript white space characters are listed in Table 2.
Table 2 — Whitespace Characters
Code Unit ValueNameFormal Name
  \u0009Tab<TAB>
  \u000BVertical Tab<VT>
  \u000CForm Feed<FF>
  \u0020Space<SP>
  \u00A0No-break space<NBSP>
  \uFEFFByte Order Mark<BOM>
  Other category "Zs"Any other Unicode
"space separator"
<USP>

ECMAScript implementations must recognize all of the white space characters defined in Unicode 3.0. Later editions of the Unicode Standard may define other white space characters. ECMAScript implementations may recognize white space characters from later editions of the Unicode Standard.

Syntax
WhiteSpace ::
<TAB>
<VT>
<FF>
<SP>
<NBSP>
<BOM>
<USP>

7.3   Line Terminators

Like white space characters, line terminator characters are used to improve source text readability and to separate tokens (indivisible lexical units) from each other. However, unlike white space characters, 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 (7.9). A line terminator cannot occur within any token except a StringLiteral. Line terminators may only occur within a StringLiteral token as part of a LineContinuation.

A line terminator can occur within a MultiLineComment (7.4) but cannot occur within a SingleLineComment.

Line terminators are included in the set of white space characters that are matched by the \s class in regular expressions.

The ECMAScript line terminator characters are listed in Table 3.

Table 3 — Line Terminator Characters
Code Unit ValueNameFormal Name
  \u000ALine Feed<LF>
  \u000DCarriage Return<CR>
  \u2028Line separator<LF>
  \u2029Paragraph separator<PS>

Only the characters in Table 3 are treated as line terminators. Other new line or line breaking characters are treated as white space but not as line terminators. The character sequence <CR><LF> is commonly used as a line terminator. It should be considered a single character for the purpose of reporting line numbers.
Syntax
LineTerminator ::
<TAB>
<VT>
<CR>
<LF>
<PS>
LineTerminatorSequence ::
<TAB>
<LF>
<CR> [lookahead <LF>]
<PS>
<CR>
<LF>

7.4   Comments

Comments can be either single or multi-line. Multi-line comments cannot nest.

Because a single-line comment can contain any character except a LineTerminator character, and because of the general rule that a token is always as long as possible, a single-line comment always consists of all characters from the // marker to the end of the line. However, the LineTerminator at the end of the line is not considered to be part of the single-line comment; it is recognized separately by the lexical grammar and becomes part of the stream of input elements for the syntactic grammar. This point is very important, because it implies that the presence or absence of single-line comments does not affect the process of automatic semicolon insertion (see 7.9 ).

Comments behave like white space and are discarded except that, if a MultiLineComment contains a line terminator character, then the entire comment is considered to be a LineTerminator for purposes of parsing by the syntactic grammar.

Syntax
MultiLineComment ::
/*   MultiLineCommentopt  */
MultiLineCommentChars ::
MultiLineNotAsteriskChar   MultiLineCommentCharsopt
* PostAsteriskCommentCharsopt
PostAsteriskCommentChars ::
MultiLineNotForwardSlashOrAsteriskChar   MultiLineCommentCharsopt
* PostAsteriskCommentCharsopt
MultiLineNotAsteriskChar ::
MultiLineNotAsteriskCharbut notasterisk *
MultiLineNotForwardSlashOrAsteriskChar ::
SourceCharacterbut not forward-slash   /or asterisk *
SingleLineComment ::
/ /  SingleLineCommentCharsopt
SingleLineCommentChars ::
SingleLineCommentChar   SingleLineCommentCharsopt
SingleLineCommentChar ::
SourceCharacter but notLineTerminator

7.5   Tokens

Syntax
Token ::
WhiteSpace
IdentifierName
Punctuator
NumericLiteral
StringLiteral
NOTE     The DivPunctuator and RegularExpressionLiteral productions define tokens, but are not included in the Token
production.

7.6   Identifier Names and Identifiers

Identifier Names are tokens that are interpreted according to the grammar given in the "Identifiers" section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1). The Unicode identifier grammar is based on both normative and informative character categories specified by the Unicode Standard. The characters in the specified categories in version 3.0 of the Unicode standard must be treated as in those categories by all conforming ECMAScript implementations.

This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

Unicode escape sequences are also permitted in an IdentifierName, where they contribute a single character to the IdentifierName, as computed by the CV of the UnicodeEscapeSequence (see 7.8.4). The \ preceding the UnicodeEscapeSequence does not contribute a character to the IdentifierName. A UnicodeEscapeSequence cannot be used to put a character into an IdentifierName that would otherwise be illegal. In other words, if a \ UnicodeEscapeSequence sequence were replaced by its UnicodeEscapeSequence's CV, the result must still be a valid IdentifierName that has the exact same sequence of characters as the original IdentifierName. All interpretations of identifiers within this specification are based upon their actual characters regardless of whether or not an escape sequence was used to contribute any particular characters.

Two IdentifierName that are canonically equivalent according to the Unicode standard are not equal unless they are represented by the exact same sequence of code units (in other words, conforming ECMAScript implementations are only required to do bitwise comparison on IdentifierName values). The intent is that the incoming source text has been converted to normalized form C before it reaches the compiler.

ECMAScript implementations may recognize identifier characters defined in later editions of the Unicode Standard. If portability is a concern, programmers should only employ identifier characters defined in Unicode 3.0.

Syntax
Identifier ::
IdentifierName
IdentifierNamebut notReservedWord
IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter>
$
_
\
UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
<ZWNJ>
<ZWJ>
UnicodeLetter
any character in the Unicode categories "Uppercase letter (Lu)", "Lowercase letter
(Ll)", "Titlecase letter (Lt)", "Modifier letter (Lm)", "Other letter (Lo)", or "Letter
number (Nl)".
UnicodeCombiningMark
any character in the Unicode categories "Non-spacing mark (Mn)" or "Combining
spacing mark (Mc)"
UnicodeDigit
any character in the Unicode category "Decimal number (Nd)"
UnicodeConnectorPunctuation
any character in the Unicode category "Connector punctuation (Pc)"
UnicodeEscapeSequence
see 7.8.4.

7.6.1   Reserved Words

A reserved word is an IdentifierName that cannot be used as an Identifier.

Syntax
ReservedWord ::
Keyword
FutureReservedWord
NullLiteral
BooleanLiteral

7.6.1.1   Keywords

The following tokens are ECMAScript keywords and may not be used as Identifiers in ECMAScript programs.

Syntax
Keyword ::one of
breakdoinstanceoftypeof
caseelsenewvar
catchfinallyreturnvoid
continueforswitchwhile
debuggerfunctionthiswith
defaultifthrow
deleteintry

7.6.1.2   Future Reserved Words

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions.

Syntax
FutureReservedWord ::one of
classenumextendssuper
constexportimport

The following tokens are also considered to be FutureReservedWords when they occur within strict mode code (see 10.1.1). The occurrence of any of these tokens within strict mode code in any context where the occurrence of a FutureReservedWord would produce an error must also produce an equivalent error:
implementsletprivatepublic
interfacepackageprotectedstatic
yield

7.7   Punctuators

Syntax

Punctuator ::one of
{}()[]
.;,<><=
>===!====!==
+-*%++--
<<>>>>>&|^
!~&&||?:
=+=-=*=%=<<=
>>=>>>=&=|=^=
DivPunctuator ::one of
//=

7.8   Literals

Syntax
Literal ::
NullLiteral
BooleanLiteral
NumericLiteral
StringLiteral
RegularExpressionLiteral

7.8.1   Null Literals

Syntax
NullLiteral ::
null
Semantics The value of the null literal null is the sole value of the Null type, namely null.

7.8.2   Boolean Literals

Syntax
BooleanLiteral ::
true
false

Semantics

The value of the Boolean literal true is a value of the Boolean type, namely true.

The value of the Boolean literal false is a value of the Boolean type, namely false.

7.8.3   Numeric Literals

Syntax
NumericLiteral ::
DecimalLiteral
HexIntegerLiteral
DecimalLiteral ::
DecimalIntegerLiteral  .  DecimalDigitsopt ExponentPartopt
 .  DecimalDigits ExponentPartopt
DecimalIntegerLiteral ExponentPartopt
DecimalIntegerLiteral ::
0
NonZeroDigit   DecimalDigitsopt
DecimalDigits ::
DecimalDigit
DecimalDigits   DecimalDigit
DecimalDigit ::one of
0  1  2  3  4  5  6  7  8  9
NonZeroDigit ::one of
1  2  3  4  5  6  7  8  9
ExponentPart ::
ExponentIndicator   SignedInteger
ExponentIndicator ::one of
e  E
SignedInteger ::
DecimalDigits
+ DecimalDigits
- DecimalDigits
HexIntegerLiteral ::
0x HexDigit
0X HexDigit
HexIntegerLiteral   HexDigit
HexDigit ::one of
0 1 2 3 4 5 6 7 8 9  a b c d e f A B C D E F

The source character immediately following a NumericLiteral must not be an IdentifierStart or DecimalDigit.
NOTE     For example:
3in
is an error and not the two input elements 3 and in.

Semantics

A numeric literal stands for a value of the Number type. This value is determined in two steps: first, a mathematical value (MV) is derived from the literal; second, this mathematical value is rounded as described below.
Once the exact MV for a numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is +0; otherwise, the rounded value must be the Number value for the MV (as specified in 8.5), unless the literal is a DecimalLiteral and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th significant digit position. A digit is significant if it is not part of an ExponentPart and A conforming implementation, when processing strict mode code (see 10.1.1), must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in B.1.1.

7.8.4   String Literals

A string literal is zero or more characters enclosed in single or double quotes. Each character may be represented by an escape sequence. All characters may appear literally in a string literal except for the closing quote character, backslash, carriage return, line separator, paragraph separator, and line feed. Any character may appear in the form of an escape sequence.

Syntax
StringLiteral ::
" DoubleStringCharactersopt"
' SingleStringCharactersopt '
DoubleStringCharacters ::
DoubleStringCharacter DoubleStringCharactersopt
SingleStringCharacters ::
SingleStringCharacter SingleStringCharactersopt
DoubleStringCharacter ::
SourceCharacter but not double-quote "  or backslash   \or LineTerminator
\ EscapeSequence
LineContinuation
SingleStringCharacter ::
SourceCharacter but not single-quote '  or backslash   \or LineTerminator
\ EscapeSequence
LineContinuation
LineContinuation ::
\ LineTerminatorSequence
EscapeSequence ::
CharacterEscapeSequence
0 [ lookahead DecimalDigit]
HexEscapeSequence
UnicodeEscapeSequence
CharacterEscapeSequence ::
SingleEscapeCharacter
NonEscapeCharacter
SingleEscapeCharacter ::one of
'  "  \  b  f  n  r  t  v
NonEscapeCharacter ::
SourceCharacter but not EscapeCharacter or LineTerminator
EscapeCharacter ::
SingleEscapeCharacter
DecimalDigit
x
u
HexEscapeSequence ::
x
HexDigit  HexDigit
UnicodeEscapeSequence ::
u
HexDigit  HexDigit  HexDigit  HexDigit
The definitions of the nonterminal HexDigit is given in 7.6. SourceCharacter is defined in clause 6.

A string literal stands for a value of the String type. The String value (SV) of the literal is described in terms of character values (CV) contributed by the various parts of the string literal. As part of this process, some characters within the string literal are interpreted as having a mathematical value (MV), as described below or in 7.8.3.

Semantics
Table 4 — String Single Character Escape Sequences
  Escape SequenceCode Unit ValueNameSymbol
\b\u0008backspace <BS>
\t\u0009horizontal tab <HT>
\n\u000Aline feed (new line) <LF>
\v\u000Bvertical tab <VT>
\f\u000Cform feed <FF>
\r\u000Dcarriage return <CR>
\"\u0022double quote "
\'\u0027single quote '
\\\u005Cbackslash \
A conforming implementation, when processing strict mode code (see 10.1.1), may not extend the syntax of EscapeSequence to include OctalEscapeSequence as described in B.1.2.
NOTE     A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.

7.8.5   Regular Expression Literals

A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated. Two regular expression literals in a program evaluate to regular expression objects that never compare as === to each other even if the two literals' contents are identical. A RegExp object may also be created at runtime by new RegExp (see 15.10.4) or calling the RegExp constructor as a function (15.10.3).

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 Strings of characters comprising the RegularExpressionBody and the RegularExpressionFlags are passed uninterpreted to the regular expression constructor, which interprets them according to its own, more stringent grammar. An implementation may extend the regular expression constructor's grammar, but it must not extend the RegularExpressionBody and RegularExpressionFlags productions or the productions used by these productions.

Syntax

RegularExpressionLiteral ::
/  RegularExpressionBody  /  RegularExpressionFlags
RegularExpressionBody ::
/  RegularExpressionFirstChar  /  RegularExpressionChars
RegularExpressionChars ::
[empty]
RegularExpressionChars  RegularExpressionChar
RegularExpressionFirstChar ::
RegularExpressionNonTerminator but not * or \ or / or[
RegularExpressionBackslashSequence
RegularExpressionClass
RegularExpressionChar ::
RegularExpressionNonTerminator but not \ or / or[
RegularExpressionBackslashSequence
RegularExpressionClass
RegularExpressionBackslashSequence ::
\ NonTerminator
RegularExpressionNonTerminator ::
SourceCharacter but not LineTerminator
RegularExpressionClass ::
[RegularExpressionClassChars]
RegularExpressionClass ::
[empty]
RegularExpressionClassChars   RegularExpressionClassChar
RegularExpressionClassChar ::
RegularExpressionNonTerminator but not ] or \
RegularExpressionBackslashSequence
RegularExpressionFlags ::
[empty]
RegularExpressionFlags   IdentifierPart
NOTE     Regular expression literals may not be empty; instead of representing an empty regular expression literal, the characters // start a single-line comment. To specify an empty regular expression, use: / ( ? : ) / .
Semantics

A regular expression literal evaluates to a value of the Object type that is an instance of the standard built-in constructor RegExp. This value is determined in two steps: first, the characters comprising the regular expression's RegularExpressionBody and RegularExpressionFlags production expansions are collected uninterpreted into two Strings Pattern and Flags, respectively. Then each time the literal is evaluated, a new object is created as if by the expression new RegExp (Pattern, Flags) where RegExp is the standard built-in constructor with that name. The newly constructed object becomes the value of the RegularExpressionLiteral. If the call to new RegExp would generate an error as specified in 15.10.4.1, the error must be treated as an early error (Clause 16).

7.9   Automatic Semicolon Insertion

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. 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.

7.9.1   Rules of Automatic Semicolon Insertion

There are three basic rules of semicolon insertion: 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 12.6.3).
NOTE     The following are the only restricted productions in the grammar:
PostfixExpression :
LeftHandSideExpression
LeftHandSideExpression [no LineTerminator here] ++
LeftHandSideExpression [no LineTerminator here] - -
ContinueStatement :
continue   [no LineTerminator here]   Identifieropt ;
BreakStatement :
break   [no LineTerminator here]   Identifieropt ;
ReturnStatement :
return   [no LineTerminator here]   Identifieropt ;
ThrowStatement :
throw   [no LineTerminator here]   Expression ;
The practical effect of these restricted productions is as follows:

When a ++ or -- token is encountered where the parser would treat it as a postfix operator, and at least one LineTerminator occurred between the preceding token and the ++ or -- token, then a semicolon is automatically inserted before the ++ or -- token.

When a continue, break, return, or throw token is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after the continue, break, return, or throw token.

The resulting practical advice to ECMAScript programmers is:

A postfix ++ or -- operator should appear on the same line as its operand.

An Expression in a return or throw statement should start on the same line as the return, or throw token.

An Identifier in a break or continue statement should be on the same line as the break or continue token.

7.9.2   Examples of Automatic Semicolon Insertion

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;
NOTE     The expression a + b is not treated as a value to be returned by the return statement, because a LineTerminator separates it from the token return.
The source
a = b
++c
is transformed by automatic semicolon insertion into the following:
a = b;
++c;
NOTE     The token ++ is not treated as a postfix operator applying to the variable b, because a LineTerminator occurs between 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.