Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1046534 of Lexical grammar

  • Revision slug: Web/JavaScript/Reference/Lexical_grammar
  • Revision title: Lexical grammar
  • Revision id: 1046534
  • Created:
  • Creator: fasttime
  • Is current revision? No
  • Comment Added keyword "of"; fixed links and sorting

Revision Content

{{JsSidebar("More")}}

This page describes JavaScript's lexical grammar. The source text of ECMAScript scripts gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments or white space. ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons to end statements.

Control characters

Control characters have no visual representation but are used to control the interpretation of the text.

Unicode format-control characters
Code point Name Abbreviation Description
U+200C Zero width non-joiner <ZWNJ> Placed between characters to prevent being connected into ligatures in certain languages (Wikipedia).
U+200D Zero width joiner <ZWJ> Placed between characters that would not normally be connected in order to cause the characters to be rendered using their connected form in certain languages (Wikipedia).
U+FEFF Byte order mark <BOM> Used at the start of the script to mark it as Unicode and the text's byte order (Wikipedia).

White space

White space characters improve the readability of source text and separate tokens from each other. These characters are usually unnecessary for the functionality of the code. Minification tools are often used to remove whitespace in order to reduce the amount of data that needs to be transferred.

White space characters
Code point Name Abbreviation Description Escape sequence
U+0009 Character tabulation <HT> Horizontal tabulation \t
U+000B Line tabulation <VT> Vertical tabulation \v
U+000C Form feed <FF> Page breaking control character (Wikipedia). \f
U+0020 Space <SP> Normal space  
U+00A0 No-break space <NBSP> Normal space, but no point at which a line may break  
Others Other Unicode space characters <USP> Spaces in Unicode on Wikipedia  

Line terminators

In addition to white space characters, line terminator characters are used to improve the readability of the source text. However, in some cases, line terminators can influence the execution of JavaScript code as there are a few places where they are forbidden. Line terminators also affect the process of automatic semicolon insertion. Line terminators are matched by the \s class in regular expressions.

Only the following Unicode code points are treated as line terminators in ECMAScript, other line breaking characters are treated as white space (for example, Next Line, NEL, U+0085 is considered as white space).

Line terminator characters
Code point Name Abbreviation Description Escape sequence
U+000A Line Feed <LF> New line character in UNIX systems. \n
U+000D Carriage Return <CR> New line character in Commodore and early Mac systems. \r
U+2028 Line Separator <LS> Wikipedia  
U+2029 Paragraph Separator <PS> Wikipedia  

Comments

Comments are used to add hints, notes, suggestions, or warnings to JavaScript code. This can make it easier to read and understand. They can also be used to disable code to prevent it from being executed; this can be a valuable debugging tool.

JavaScript has two ways of assigning comments in its code.

The first way is the // comment; this makes all text following it on the same line into a comment. For example:

function comment() {
  // This is a one line JavaScript comment
  console.log("Hello world!");
}
comment();

The second way is the /* */ style, which is much more flexible.

For example, you can use it on a single line:

function comment() {
  /* This is a one line JavaScript comment */
  console.log("Hello world!");
}
comment();

You can also make multiple-line comments, like this:

function comment() {
  /* This comment spans multiple lines. Notice
     that we don't need to end the comment until we're done. */
  console.log("Hello world!");
}
comment();

You can also use it in the middle of a line, if you wish, although this can make your code harder to read so it should be used with caution:

function comment(x) {
  console.log("Hello " + x /* insert the value of x */ + " !");
}
comment("world");

In addition, you can use it to disable code to prevent it from running, by wrapping code in a comment, like this:

function comment() {
  /* console.log("Hello world!"); */
}
comment();

In this case, the console.log() call is never issued, since it's inside a comment. Any number of lines of code can be disabled this way.

Keywords

Reserved keywords as of ECMAScript 6

  • {{jsxref("Statements/break", "break")}}
  • {{jsxref("Statements/switch", "case")}}
  • {{jsxref("Statements/try...catch", "catch")}}
  • {{jsxref("Statements/class", "class")}}
  • {{jsxref("Statements/const", "const")}}
  • {{jsxref("Statements/continue", "continue")}}
  • {{jsxref("Statements/debugger", "debugger")}}
  • {{jsxref("Statements/default", "default")}}
  • {{jsxref("Operators/delete", "delete")}}
  • {{jsxref("Statements/do...while", "do")}}
  • {{jsxref("Statements/if...else", "else")}}
  • {{jsxref("Statements/export", "export")}}
  • {{jsxref("Statements/class", "extends")}}
  • {{jsxref("Statements/try...catch", "finally")}}
  • {{jsxref("Statements/for", "for")}}
  • {{jsxref("Statements/function", "function")}}
  • {{jsxref("Statements/if...else", "if")}}
  • {{jsxref("Statements/import", "import")}}
  • {{jsxref("Operators/in", "in")}}
  • {{jsxref("Operators/instanceof", "instanceof")}}
  • {{jsxref("Operators/new", "new")}}
  • {{jsxref("Operators/for...of", "of")}}
  • {{jsxref("Statements/return", "return")}}
  • {{jsxref("Operators/super", "super")}}
  • {{jsxref("Statements/switch", "switch")}}
  • {{jsxref("Operators/this", "this")}}
  • {{jsxref("Statements/throw", "throw")}}
  • {{jsxref("Statements/try...catch", "try")}}
  • {{jsxref("Operators/typeof", "typeof")}}
  • {{jsxref("Statements/var", "var")}}
  • {{jsxref("Operators/void", "void")}}
  • {{jsxref("Statements/while", "while")}}
  • {{jsxref("Statements/with", "with")}}
  • {{jsxref("Operators/yield", "yield")}}

Future reserved keywords

The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.

These are always reserved:

  • enum

The following are only reserved when they are found in strict mode code:

  • implements
  • interface
  • {{jsxref("Statements/let", "let")}}
  • package
  • protected
  • static
  • private
  • public

The following are only reserved when they are found in module code:

  • await

Future reserved keywords in older standards

The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).

  • abstract
  • boolean
  • byte
  • char
  • double
  • final
  • float
  • goto
  • int
  • long
  • native
  • short
  • synchronized
  • throws
  • transient
  • volatile

Additionally, the literals null, true, and false cannot be used as identifiers in ECMAScript.

Reserved word usage

Reserved words actually only apply to Identifiers (vs. IdentifierNames) . As described in es5.github.com/#A.1, these are all IdentifierNames which do not exclude ReservedWords.

a.import
a["import"]
a = { import: "test" }.

On the other hand the following is illegal because it's an Identifier, which is an IdentifierName without the reserved words. Identifiers are used for FunctionDeclaration and FunctionExpression.

function import() {} // Illegal.

Literals

Null literal

See also null for more information.

null

Boolean literal

See also Boolean for more information.

true
false

Numeric literals

Decimal

1234567890
42

// Caution when using with a leading zero:
0888 // 888 parsed as decimal
0777 // parsed as octal, 511 in decimal

Note that decimal literals can start with a zero (0) followed by another decimal digit, but If all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number. This won't throw in JavaScript, see {{bug(957513)}}. See also the page about parseInt().

Binary

Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (0b or 0B). Because this syntax is new in ECMAScript 6, see the browser compatibility table, below. If the digits after the 0b are not 0 or 1, the following SyntaxError is thrown: "Missing binary digits after 0b".

var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607

Octal

Octal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (0o or 0O). Because this syntax is new in ECMAScript 6, see the browser compatibility table, below. If the digits after the 0o are outside the range (01234567), the following SyntaxError is thrown:  "Missing octal digits after 0o".

var n = 0O755; // 493
var m = 0o644; // 420

// Also possible with just a leading zero (see note about decimals above)
0755
0644

Hexadecimal

Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X). If the digits after 0x are outside the range (0123456789ABCDEF),  the following SyntaxError is thrown: "Identifier starts immediately after numeric literal".

0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10

Object literals

See also {{jsxref("Object")}} and Object initializer for more information.

var o = { a: "foo", b: "bar", c: 42 };

// shorthand notation. New in ES6
var a = "foo", b = "bar", c = 42;
var o = {a, b, c};

// instead of
var o = { a: a, b: b, c: c };

Array literals

See also {{jsxref("Array")}} for more information.

[1954, 1974, 1990, 2014]

String literals

'foo'
"bar"

Hexadecimal escape sequences

'\xA9' // "©"

Unicode escape sequences

The Unicode escape sequences require at least four characters following \u.

'\u00A9' // "©"

Unicode code point escapes

New in ECMAScript 6. With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF. With simple Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same.

See also {{jsxref("String.fromCodePoint()")}} or {{jsxref("String.prototype.codePointAt()")}}.

'\u{2F804}'

// the same with simple Unicode escapes
'\uD87E\uDC04'

Regular expression literals

See also RegExp for more information.

/ab+c/g

// An "empty" regular expression literal
// The empty non-capturing group is necessary 
// to avoid ambiguity with single-line comments.
/(?:)/

Template literals

See also template strings for more information.

`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

tag `string text ${expression} string text`

Automatic semicolon insertion

Some JavaScript statements must be terminated with semicolons and are therefore affected by automatic semicolon insertion (ASI):

  • Empty statement
  • let, const, variable statement
  • import, export, module declaration
  • Expression statement
  • debugger
  • continue, break, throw
  • return

The ECMAScript specification mentions three rules of semicolon insertion.

1.  A semicolon is inserted before, when a Line terminator or "}" is encountered that is not allowed by the grammar.

{ 1 2 } 3 

// is transformed by ASI into 

{ 1 2 ;} 3;

2.  A semicolon is inserted at the end, when the end of the input stream of tokens is detected and the parser is unable to parse the single input stream as a complete program.

Here ++ is not treated as a postfix operator applying to variable b, because a line terminator occurs between b and ++.

a = b
++c

// is transformend by ASI into

a = b;
++c;

3. A semicolon is inserted at the end, when a statement with restricted productions in the grammar is followed by a line terminator. These statements with "no LineTerminator here" rules are:

  • PostfixExpressions (++ and --)
  • continue
  • break
  • return
  • yield, yield*
  • module
return
a + b

// is transformed by ASI into

return;
a + b;

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2("ES1")}} Initial definition.
{{SpecName('ES5.1', '#sec-7', 'Lexical Conventions')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-ecmascript-language-lexical-grammar', 'Lexical Grammar')}} {{Spec2('ES6')}} Added: Binary and Octal Numeric literals, Unicode code point escapes, Templates
{{SpecName('ESDraft', '#sec-ecmascript-language-lexical-grammar', 'Lexical Grammar')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Binary and octal numeric literals
(0b and 0o)
{{CompatChrome(41)}} 12 {{CompatGeckoDesktop(25)}} {{CompatUnknown}} {{CompatOpera(28)}} {{CompatSafari(9)}}
Unicode code point escapes
(\u{})
{{CompatChrome(44)}} 12 {{CompatGeckoDesktop(40)}} {{CompatNo}} {{CompatOpera(31)}} {{CompatSafari(9)}}
Shorthand notation for object literals {{CompatChrome(43)}} 12 {{CompatGeckoDesktop(33)}} {{CompatNo}} {{CompatChrome(30)}} {{CompatSafari(9)}}
Template literals {{CompatChrome(41)}} 12 {{CompatGeckoDesktop(34)}} {{CompatNo}} {{CompatOpera(28)}} {{CompatSafari(9)}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Binary and octal numeric literals (0b and 0o) {{CompatUnknown}} 41 {{CompatGeckomobile(33)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Unicode code point escapes (\u{}) {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckomobile(40)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Shorthand notation for object literals {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(33)}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Template literals {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(34)}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

Firefox-specific notes

  • Prior to Firefox 5 (JavaScript 1.8.6), future reserved keywords could be used when not in strict mode. This ECMAScript violation was fixed in Firefox 5.

See also

Revision Source

<div>{{JsSidebar("More")}}</div>

<p>This page describes JavaScript's lexical grammar. The source text of ECMAScript scripts gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments or white space. ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons to end statements.</p>

<h2 id="Control_characters">Control characters</h2>

<p>Control characters have no visual representation but are used to control the interpretation of the text.</p>

<table class="standard-table">
 <caption>Unicode format-control characters</caption>
 <tbody>
  <tr>
   <th>Code point</th>
   <th>Name</th>
   <th>Abbreviation</th>
   <th>Description</th>
  </tr>
  <tr>
   <td><code>U+200C</code></td>
   <td>Zero width non-joiner</td>
   <td>&lt;ZWNJ&gt;</td>
   <td>Placed between characters to prevent being connected into ligatures in certain languages (<a href="https://en.wikipedia.org/wiki/Zero-width_non-joiner">Wikipedia</a>).</td>
  </tr>
  <tr>
   <td><code>U+200D</code></td>
   <td>Zero width joiner</td>
   <td>&lt;ZWJ&gt;</td>
   <td>Placed between characters that would not normally be connected in order to cause the characters to be rendered using their connected form in certain languages (<a href="https://en.wikipedia.org/wiki/Zero-width_joiner">Wikipedia</a>).</td>
  </tr>
  <tr>
   <td><code>U+FEFF</code></td>
   <td>Byte order mark</td>
   <td>&lt;BOM&gt;</td>
   <td>Used at the start of the script to mark it as Unicode and the text's byte order (<a href="https://en.wikipedia.org/wiki/Byte_order_mark">Wikipedia</a>).</td>
  </tr>
 </tbody>
</table>

<h2 id="White_space">White space</h2>

<p>White space characters improve the readability of source text and separate tokens from each other. These characters are usually unnecessary for the functionality of the code. <a href="https://en.wikipedia.org/wiki/Minification_%28programming%29">Minification tools</a> are often used to remove whitespace in order to reduce the amount of data that needs to be transferred.</p>

<table class="standard-table">
 <caption>White space characters</caption>
 <tbody>
  <tr>
   <th>Code point</th>
   <th>Name</th>
   <th>Abbreviation</th>
   <th>Description</th>
   <th>Escape sequence</th>
  </tr>
  <tr>
   <td>U+0009</td>
   <td>Character tabulation</td>
   <td>&lt;HT&gt;</td>
   <td>Horizontal tabulation</td>
   <td>\t</td>
  </tr>
  <tr>
   <td>U+000B</td>
   <td>Line tabulation</td>
   <td>&lt;VT&gt;</td>
   <td>Vertical tabulation</td>
   <td>\v</td>
  </tr>
  <tr>
   <td>U+000C</td>
   <td>Form feed</td>
   <td>&lt;FF&gt;</td>
   <td>Page breaking control character (<a href="https://en.wikipedia.org/wiki/Page_break#Form_feed">Wikipedia</a>).</td>
   <td>\f</td>
  </tr>
  <tr>
   <td>U+0020</td>
   <td>Space</td>
   <td>&lt;SP&gt;</td>
   <td>Normal space</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>U+00A0</td>
   <td>No-break space</td>
   <td>&lt;NBSP&gt;</td>
   <td>Normal space, but no point at which a line may break</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>Others</td>
   <td>Other Unicode space characters</td>
   <td>&lt;USP&gt;</td>
   <td><a href="https://en.wikipedia.org/wiki/Space_%28punctuation%29#Spaces_in_Unicode">Spaces in Unicode on Wikipedia</a></td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Line_terminators">Line terminators</h2>

<p>In addition to white space characters, line terminator characters are used to improve the readability of the source text. However, in some cases, line terminators can influence the&nbsp;execution of JavaScript code as there are a few places where they are forbidden. Line terminators also affect the process of <a href="#Automatic_semicolon_insertion">automatic semicolon insertion</a>. Line terminators are matched by the <strong>\s</strong> class in <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">regular expressions</a>.</p>

<p>Only the following Unicode code points are treated as line terminators in ECMAScript, other line breaking characters are treated as white space (for example, Next Line, NEL, U+0085 is considered as white space).</p>

<table class="standard-table">
 <caption>Line terminator characters</caption>
 <tbody>
  <tr>
   <th>Code point</th>
   <th>Name</th>
   <th>Abbreviation</th>
   <th>Description</th>
   <th>Escape sequence</th>
  </tr>
  <tr>
   <td>U+000A</td>
   <td>Line Feed</td>
   <td>&lt;LF&gt;</td>
   <td>New line character in UNIX systems.</td>
   <td>\n</td>
  </tr>
  <tr>
   <td>U+000D</td>
   <td>Carriage Return</td>
   <td>&lt;CR&gt;</td>
   <td>New line character in Commodore and early Mac systems.</td>
   <td>\r</td>
  </tr>
  <tr>
   <td>U+2028</td>
   <td>Line Separator</td>
   <td>&lt;LS&gt;</td>
   <td><a href="https://en.wikipedia.org/wiki/Newline">Wikipedia</a></td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>U+2029</td>
   <td>Paragraph Separator</td>
   <td>&lt;PS&gt;</td>
   <td><a href="https://en.wikipedia.org/wiki/Newline">Wikipedia</a></td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Comments">Comments</h2>

<p>Comments are used to add hints, notes, suggestions, or warnings to JavaScript code. This can make it easier to read and understand. They can also be used to disable code to prevent it from being executed; this can be a valuable debugging tool.</p>

<p>JavaScript has two ways of assigning comments in its code.</p>

<p>The first way is the <code>//</code> comment; this makes all text following it on the same line into a comment. For example:</p>

<pre class="brush: js">
function comment() {
&nbsp; // This is a one line JavaScript comment
&nbsp; console.log("Hello world!");
}
comment();
</pre>

<p>The second way is the <code>/* */</code> style, which is much more flexible.</p>

<p>For example, you can use it on a single line:</p>

<pre class="brush: js">
function comment() {
&nbsp; /* This is a one line JavaScript comment */
&nbsp; console.log("Hello world!");
}
comment();</pre>

<p>You can also make multiple-line comments, like this:</p>

<pre class="brush: js">
function comment() {
&nbsp; /*&nbsp;This comment spans multiple lines. Notice
&nbsp;&nbsp;&nbsp;&nbsp; that we don't need to end the comment until we're done. */
&nbsp; console.log("Hello world!");
}
comment();</pre>

<p>You can also use it in the middle of a line, if you wish, although this can make your code harder to read so it should be used with caution:</p>

<pre class="brush: js">
function comment(x) {
&nbsp; console.log("Hello " + x /* insert the value of x */ +&nbsp;" !");
}
comment("world");</pre>

<p>In addition, you can use it to disable code to prevent it from running, by wrapping code in a comment, like this:</p>

<pre class="brush: js">
function comment() {
&nbsp; /* console.log("Hello world!"); */
}
comment();</pre>

<p>In this case, the <code>console.log()</code> call is never issued, since it's inside a comment. Any number of lines of code can be disabled this way.</p>

<h2 id="Keywords">Keywords</h2>

<h3 id="Reserved_keywords_as_of_ECMAScript_6">Reserved keywords as of ECMAScript 6</h3>

<ul class="threecolumns">
 <li>{{jsxref("Statements/break", "break")}}</li>
 <li>{{jsxref("Statements/switch", "case")}}</li>
 <li>{{jsxref("Statements/try...catch", "catch")}}</li>
 <li>{{jsxref("Statements/class", "class")}}</li>
 <li>{{jsxref("Statements/const", "const")}}</li>
 <li>{{jsxref("Statements/continue", "continue")}}</li>
 <li>{{jsxref("Statements/debugger", "debugger")}}</li>
 <li>{{jsxref("Statements/default", "default")}}</li>
 <li>{{jsxref("Operators/delete", "delete")}}</li>
 <li>{{jsxref("Statements/do...while", "do")}}</li>
 <li>{{jsxref("Statements/if...else", "else")}}</li>
 <li>{{jsxref("Statements/export", "export")}}</li>
 <li>{{jsxref("Statements/class", "extends")}}</li>
 <li>{{jsxref("Statements/try...catch", "finally")}}</li>
 <li>{{jsxref("Statements/for", "for")}}</li>
 <li>{{jsxref("Statements/function", "function")}}</li>
 <li>{{jsxref("Statements/if...else", "if")}}</li>
 <li>{{jsxref("Statements/import", "import")}}</li>
 <li>{{jsxref("Operators/in", "in")}}</li>
 <li>{{jsxref("Operators/instanceof", "instanceof")}}</li>
 <li>{{jsxref("Operators/new", "new")}}</li>
 <li>{{jsxref("Operators/for...of", "of")}}</li>
 <li>{{jsxref("Statements/return", "return")}}</li>
 <li>{{jsxref("Operators/super", "super")}}</li>
 <li>{{jsxref("Statements/switch", "switch")}}</li>
 <li>{{jsxref("Operators/this", "this")}}</li>
 <li>{{jsxref("Statements/throw", "throw")}}</li>
 <li>{{jsxref("Statements/try...catch", "try")}}</li>
 <li>{{jsxref("Operators/typeof", "typeof")}}</li>
 <li>{{jsxref("Statements/var", "var")}}</li>
 <li>{{jsxref("Operators/void", "void")}}</li>
 <li>{{jsxref("Statements/while", "while")}}</li>
 <li>{{jsxref("Statements/with", "with")}}</li>
 <li>{{jsxref("Operators/yield", "yield")}}</li>
</ul>

<h3 id="Future_reserved_keywords">Future reserved keywords</h3>

<p>The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.</p>

<p>These are always reserved:</p>

<ul>
 <li><code>enum</code></li>
</ul>

<p>The following are only reserved when they are found in strict mode code:</p>

<ul class="threecolumns">
 <li><code>implements</code></li>
 <li><code>interface</code></li>
 <li>{{jsxref("Statements/let", "let")}}</li>
 <li><code>package</code></li>
 <li><code>protected</code></li>
 <li><code>static</code></li>
 <li><code>private</code></li>
 <li><code>public</code></li>
</ul>

<p>The following are only reserved when they are found in module code:</p>

<ul>
 <li><code>await</code></li>
</ul>

<h4 id="Future_reserved_keywords_in_older_standards">Future reserved keywords in older standards</h4>

<p>The following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).</p>

<ul class="threecolumns">
 <li><code>abstract</code></li>
 <li><code>boolean</code></li>
 <li><code>byte</code></li>
 <li><code>char</code></li>
 <li><code>double</code></li>
 <li><code>final</code></li>
 <li><code>float</code></li>
 <li><code>goto</code></li>
 <li><code>int</code></li>
 <li><code>long</code></li>
 <li><code>native</code></li>
 <li><code>short</code></li>
 <li><code>synchronized</code></li>
 <li><code>throws</code></li>
 <li><code>transient</code></li>
 <li><code>volatile</code></li>
</ul>

<p>Additionally, the literals <code>null</code>, <code>true</code>, and <code>false</code> cannot be used as identifiers in ECMAScript.</p>

<h3 id="Reserved_word_usage">Reserved word usage</h3>

<p>Reserved words actually only apply to Identifiers (vs. <code>IdentifierNames</code>) . As described in <a href="https://es5.github.com/#A.1">es5.github.com/#A.1</a>, these are all <code>IdentifierNames</code> which do not exclude <code>ReservedWords</code>.</p>

<pre class="brush: js">
a.import
a["import"]
a = { import: "test" }.
</pre>

<p>On the other hand the following is illegal because it's an Identifier, which is an <code>IdentifierName</code> without the reserved words. Identifiers are used for <code>FunctionDeclaration</code> and <code>FunctionExpression</code>.</p>

<pre class="brush: js">
function import() {} // Illegal.</pre>

<h2 id="Literals">Literals</h2>

<h3 id="Null_literal">Null literal</h3>

<p>See also <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/null"><code>null</code></a> for more information.</p>

<pre class="brush: js">
null</pre>

<h3 id="Boolean_literal">Boolean literal</h3>

<p>See also <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean"><code>Boolean</code></a> for more information.</p>

<pre class="brush: js">
true
false</pre>

<h3 id="Numeric_literals">Numeric literals</h3>

<h4 id="Decimal">Decimal</h4>

<pre class="brush: js">
1234567890
42

// Caution when using with a leading zero:
0888 // 888 parsed as decimal
0777 // parsed as octal, 511 in decimal
</pre>

<p>Note that decimal literals can start with a zero (<code>0</code>) followed by another decimal digit, but If all digits after the leading <code>0</code> are smaller than 8, the number is interpreted as an octal number. This won't throw in JavaScript, see {{bug(957513)}}. See also the page about <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Octal_interpretations_with_no_radix"><code>parseInt()</code></a>.</p>

<h4 id="Binary">Binary</h4>

<p>Binary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (<code>0b</code> or <code>0B</code>). Because this syntax is new in ECMAScript 6,&nbsp;see the browser compatibility table, below. If the digits after the <code>0b</code> are not 0 or 1, the following&nbsp;<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code>&nbsp;is thrown: "Missing binary digits after 0b".</p>

<pre class="brush: js">
var FLT_SIGNBIT  = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0B00000000011111111111111111111111; // 8388607</pre>

<h4 id="Octal">Octal</h4>

<p>Octal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (<code>0o</code> or <code>0O)</code>. Because this syntax is new in ECMAScript 6, see the browser compatibility table, below. If the digits after the <code>0o</code> are outside the range (01234567), the following <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code>&nbsp;is thrown:&nbsp; "Missing octal digits after 0o".</p>

<pre class="brush: js">
var n = 0O755; // 493
var m = 0o644; // 420

// Also possible with just a leading zero (see note about decimals above)
0755
0644
</pre>

<h4 id="Hexadecimal">Hexadecimal</h4>

<p>Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (<code>0x</code> or <code>0X)</code>. If the digits after 0x are outside the range&nbsp;(0123456789ABCDEF),&nbsp;&nbsp;the following <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a></code> is thrown: "Identifier starts immediately after numeric literal".</p>

<pre class="brush: js">
0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10
</pre>

<h3 id="Object_literals">Object literals</h3>

<p>See also {{jsxref("Object")}} and <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">Object initializer</a> for more information.</p>

<pre class="brush: js">
var o = { a: "foo", b: "bar", c: 42 };

// shorthand notation. New in ES6
var a = "foo", b = "bar", c = 42;
var o = {a, b, c};

// instead of
var o = { a: a, b: b, c: c };
</pre>

<h3 id="Array_literals">Array literals</h3>

<p>See also {{jsxref("Array")}} for more information.</p>

<pre class="brush: js">
[1954, 1974, 1990, 2014]</pre>

<h3 id="String_literals">String literals</h3>

<pre class="brush: js">
'foo'
"bar"</pre>

<h4 id="Hexadecimal_escape_sequences">Hexadecimal escape sequences</h4>

<pre class="brush: js">
'\xA9' // "©"
</pre>

<h4 id="Unicode_escape_sequences">Unicode escape sequences</h4>

<p>The Unicode escape sequences require at least four characters following <code>\u</code>.</p>

<pre class="brush: js">
'\u00A9' // "©"</pre>

<h4 id="Unicode_code_point_escapes">Unicode code point escapes</h4>

<p>New in ECMAScript 6. With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to <code>0x10FFFF</code>. With simple Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same.</p>

<p>See also {{jsxref("String.fromCodePoint()")}} or {{jsxref("String.prototype.codePointAt()")}}.</p>

<pre class="brush: js">
'\u{2F804}'

// the same with simple Unicode escapes
'\uD87E\uDC04'</pre>

<h3 id="Regular_expression_literals">Regular expression literals</h3>

<p>See also <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp</code></a> for more information.</p>

<pre class="brush: js">
/ab+c/g

// An "empty" regular expression literal
// The empty non-capturing group is necessary 
// to avoid ambiguity with single-line comments.
/(?:)/</pre>

<h3 id="Template_literals">Template literals</h3>

<p>See also <a href="/en-US/docs/Web/JavaScript/Reference/template_strings">template strings</a> for more information.</p>

<pre class="brush: js">
`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

tag `string text ${expression} string text`</pre>

<h2 id="Automatic_semicolon_insertion">Automatic semicolon insertion</h2>

<p>Some <a href="/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript statements</a> must be terminated with semicolons and are therefore affected by automatic semicolon insertion (ASI):</p>

<ul>
 <li>Empty statement</li>
 <li><code>let</code>, <code>const</code>, variable statement</li>
 <li><code>import</code>, <code>export</code>, module declaration</li>
 <li>Expression statement</li>
 <li><code>debugger</code></li>
 <li><code>continue</code>, <code>break</code>, <code>throw</code></li>
 <li><code>return</code></li>
</ul>

<p>The ECMAScript specification mentions<a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-rules-of-automatic-semicolon-insertion"> three rules of semicolon insertion</a>.</p>

<p>1.&nbsp; A semicolon is inserted before, when a <a href="#Line_terminators">Line terminator</a> or "}" is encountered that is not allowed by the grammar.</p>

<pre class="brush: js">
{ 1 2 } 3 

// is transformed by ASI into 

{ 1 2 ;} 3;</pre>

<p>2.&nbsp; A semicolon is inserted at the end, when the end of the input stream of tokens is detected and the&nbsp;parser is unable to parse the single input stream as a complete program.</p>

<p>Here <code>++</code> is not treated as a <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment">postfix operator</a> applying to variable <code>b</code>, because a line terminator occurs between <code>b</code> and <code>++</code>.</p>

<pre class="brush: js">
a = b
++c

// is transformend by ASI into

a = b;
++c;
</pre>

<p>3. A semicolon is inserted at the end, when a statement with restricted productions in the grammar is followed by a line terminator. These statements with "no LineTerminator here" rules are:</p>

<ul>
 <li>PostfixExpressions (<code>++</code> and <code>--</code>)</li>
 <li><code>continue</code></li>
 <li><code>break</code></li>
 <li><code>return</code></li>
 <li><code>yield</code>, <code>yield*</code></li>
 <li><code>module</code></li>
</ul>

<pre class="brush: js">
return
a + b

// is transformed by ASI into

return;
a + b;
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2("ES1")}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-7', 'Lexical Conventions')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-ecmascript-language-lexical-grammar', 'Lexical Grammar')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Added: Binary and Octal Numeric literals, Unicode code point escapes, Templates</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-ecmascript-language-lexical-grammar', 'Lexical Grammar')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Binary and octal numeric literals<br />
    (<code>0b</code> and <code>0o</code>)</td>
   <td>{{CompatChrome(41)}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop(25)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatOpera(28)}}</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
  <tr>
   <td>Unicode code point escapes<br />
    (<code>\u{}</code>)</td>
   <td>{{CompatChrome(44)}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop(40)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera(31)}}</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
  <tr>
   <td>Shorthand notation for object literals</td>
   <td>{{CompatChrome(43)}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop(33)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(30)}}</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
  <tr>
   <td>Template literals</td>
   <td>{{CompatChrome(41)}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop(34)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera(28)}}</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Binary and octal numeric literals (<code>0b</code> and <code>0o</code>)</td>
   <td>{{CompatUnknown}}</td>
   <td>41</td>
   <td>{{CompatGeckomobile(33)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Unicode code point escapes (<code>\u{}</code>)</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckomobile(40)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Shorthand notation for object literals</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(33)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Template literals</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(34)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<ul>
 <li>Prior to Firefox 5 (JavaScript 1.8.6), future reserved keywords could be used when not in strict mode. This ECMAScript violation was fixed in Firefox 5.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://whereswalden.com/2013/08/12/micro-feature-from-es6-now-in-firefox-aurora-and-nightly-binary-and-octal-numbers/">Jeff Walden: Binary and octal numbers</a></li>
 <li><a href="https://mathiasbynens.be/notes/javascript-escapes">Mathias Bynens: JavaScript character escape sequences</a></li>
 <li>{{jsxref("Boolean")}}</li>
 <li>{{jsxref("Number")}}</li>
 <li>{{jsxref("RegExp")}}</li>
 <li>{{jsxref("String")}}</li>
</ul>
Revert to this revision