View Discussion Table of Contents Improve Article Save Article View Discussion Improve Article Save Article
Reserved Words:
Reserved Words added in ECMAScript 5 and 6.
Removed Reserved Words Following reserved words has been removed from ECMAScript 5 and 6.
This page describes JavaScript's lexical grammar. JavaScript source text is just a sequence of characters — in order for the interpreter to understand it, the string has to be parsed to a more structured representation. The initial step of parsing is called lexical analysis, in which the text gets scanned from left to right and is converted into a sequence of individual, atomic input elements. Some input elements are insignificant to the interpreter, and will be stripped after this step — they include control characters, line terminators, white space, and comments. The others, such as identifiers and punctuators, will be used for further syntax analysis. ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons to make certain invalid token sequences become valid. Format-control charactersFormat-control characters have no visual representation but are used to control the interpretation of the text. In JavaScript source text, <ZWNJ> and <ZWJ> are treated as identifier parts, while <BOM> (also called a zero-width no-break space <ZWNBSP> when not at the start of text) is treated as whitespace. White spaceWhite 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. Line terminatorsIn 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 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). 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 long-standing ways to add comments to code. The first way is the
The second way is the For example, you can use it on a single line:
You can also make multiple-line comments, like this:
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:
In addition, you can use it to disable code to prevent it from running, by wrapping code in a comment, like this:
In this case, the There's a special third comment syntax, the hashbang
comment. A hashbang comment behaves exactly like a single line-only ( Hashbang comments in JavaScript resemble shebangs in Unix which provide the path to a specific JavaScript interpreter that you want to use to execute the script. Before the hashbang comment became standardized, it had already been de-facto implemented in non-browser hosts like Node.js, where it was stripped from the source text before being passed to the engine. An example is as follows:
The JavaScript interpreter will treat it as a normal comment — it only has semantic meaning to the shell if the script is directly run in a shell. Warning: If you want scripts to be runnable directly in a shell environment, encode them in UTF-8 without a BOM. Although a BOM will not cause any problems for code running in a browser, it is not advised to use a BOM with a hashbang in a script — because the BOM will prevent the script from working when you try to run it in a Unix/Linux shell environment. So if you want scripts to be runnable directly in a shell environment, encode them in UTF-8 without a BOM. You must only use the IdentifiersAn identifier is used to link a value with a name. Identifiers can be used in various places:
In JavaScript, identifiers are commonly made of alphanumeric characters, underscores ( Note: If, for some reason, you need to parse some JavaScript source yourself, do not assume all identifiers follow the pattern In addition, JavaScript allows using Unicode escape sequences in the form of
Not all places accept the full range of identifiers. Certain syntaxes, such as function declarations, function expressions, and variable declarations require using identifiers names that are not reserved words.
Most notably, private properties and object properties allow reserved words.
KeywordsKeywords are tokens that look like
identifiers but have special meanings in JavaScript. For example, the keyword Some keywords are reserved, meaning that cannot be used as an identifier for variable declarations, function declarations, etc. They are often called reserved words. A list of these reserved words is provided below. Not all keywords are reserved — for example, Identifiers are always compared by string value, so escape sequences are interpreted. For example, this is still a syntax error:
Reserved wordsThese keywords cannot be used as identifiers for variables, functions, classes, etc. anywhere in JavaScript source.
The following are only reserved when they are found in strict mode code:
The following are only reserved when they are found in module code or async function bodies:
Future reserved wordsThe 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:
The following are only reserved when they are found in strict mode code:
Future reserved words in older standardsThe following are reserved as future keywords by older ECMAScript specifications (ECMAScript 1 till 3).
Identifiers with special meaningsA few identifiers have a special meaning in some contexts without being reserved words of any kind. They include:
LiteralsNull literalSee also Boolean literalSee also boolean type for more information. Numeric literalsThe Number and BigInt types use numeric literals. Decimal
Note that decimal literals can start with a zero ( ExponentialThe decimal exponential literal is specified by the following format:
BinaryBinary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" (
OctalOctal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (
HexadecimalHexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (
BigInt literalThe BigInt type is a numeric primitive in JavaScript that can
represent integers with arbitrary precision. BigInt literals are created by appending
Note that legacy octal numbers with just a leading zero won't work for
For octal For more information about Numeric separatorsTo improve readability for numeric literals, underscores
(
Note these limitations:
Object literalsSee also
Array literalsSee also String literalsA string literal is zero 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 these closing quote code points:
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. Hexadecimal escape sequencesHexadecimal escape sequences consist of Unicode escape sequencesA Unicode escape sequence consists of exactly four hexadecimal digits following See also Unicode code point escapesA Unicode code point escape consists of See also
Regular expression literalsSee also
Template literalsSee also template strings for more information.
Automatic semicolon insertionSome JavaScript statements must be terminated with semicolons and are therefore affected by automatic semicolon insertion (ASI):
There are three cases when semicolons are automatically inserted: 1. When a token not allowed by the grammar is encountered, and it's separated from the previous token by at least one line terminator, or the token is "}", then a semicolon is inserted before the token.
The ending ")" of
2. When the end of the input stream of tokens is reached, and the parser is unable to parse the single input stream as a complete program, a semicolon is inserted at the end.
This rule is a complement to the previous rule, specifically for the case where there's no "offending token" but the end of input stream. 3. When the grammar forbids line terminators in some place but a line terminator is found, a semicolon is inserted. These places include:
Here
Here the
Note that ASI would only be triggered if a line break separates tokens that would otherwise produce invalid syntax. If the next token can be parsed as part of a valid structure, semicolons would not be inserted. For example:
Because
Therefore, you would get errors like "1 is not a function" and "Cannot read properties of undefined (reading 'forEach')" when running the code. Within classes, class fields and generator methods can be a pitfall as well.
It is seen as:
And therefore will be a syntax error around There are the following rules-of-thumb for dealing with ASI, if you want to enforce semicolon-less style:
Browser compatibilityBCD tables only load in the browser See alsoIs array a reserved word in JavaScript?Some SPECIAL words such as Object , Array , Function , Method , Number etc are not belong to keywords in Javascrpt: Reserved Keywords in Javascript. Is short a reserved word in JavaScript?It does nothing. It's reserved for (potential) future use but currently it's not in the specs. Is true a reserved word in JavaScript?JavaScript keywords: break, case, catch, continue, debugger, default, delete, do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, with . What is a reserved word examples?Often found in programming languages and macros, reserved words are terms or phrases appropriated for special use that may not be utilized in the creation of variable names. For example, "print" is a reserved word because it is a function in many languages to show text on the screen. |