GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing developer.mozilla.org — Lexical grammar - JavaScript | MDN Lexical grammarThis 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 white space and comments. The others, including identifiers, keywords, literals, and punctuators (mostly operators), will be used for further syntax analysis. Line terminators and multiline comments are also syntactically insignificant, but they guide the process for automatic semicolon insertion 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 white space. 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.
Note: Of those characters with the "White_Space" property but are not in the "Space_Separator" general category, U+0009, U+000B, and U+000C are still treated as white space in JavaScript; U+0085 NEXT LINE has no special role; others become the set of line terminators. Note:
Changes to the Unicode standard used by the JavaScript engine may affect programs' behavior. For example, ES2016 upgraded the reference Unicode standard from 5.1 to 8.0.0, which caused U+180E MONGOLIAN VOWEL SEPARATOR to be moved from the "Space_Separator" category to the "Format (Cf)" category, and made it a non-whitespace. Subsequently, the result of 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. Outside the context of lexical grammar, white space and line terminators are often conflated. For example, Only the following Unicode code points are treated as line terminators in ECMAScript. Other line breaking characters are treated as white space (for example, Line Tabulation, VT, U+000B). CommentsComments 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: line comments and block comments. In addition, there's a special hashbang comment syntax. Line commentsThe first way is the js Block commentsThe second way is the For example, you can use it on a single line: js You can also make multiple-line comments, like this: js 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: js In addition, you can use it to disable code to prevent it from running, by wrapping code in a comment, like this: js In this case, the Block comments that contain at least one line terminator behave like line terminators in automatic semicolon insertion. Hashbang commentsThere'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: js 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 — because it's stripped during UTF-8 decoding, before the source text is analyzed — a Unix/Linux shell will not recognize the hashbang if it's preceded by a BOM character. You must only use the IdentifiersAn identifier is used to link a value with a name. Identifiers can be used in various places: js 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 js Not all places accept the full range of identifiers. Certain syntaxes, such as function declarations, function expressions, and variable declarations require using identifier names that are not reserved words. js Most notably, private elements and object properties allow reserved words. js KeywordsKeywords are tokens that look like identifiers but have special meanings in JavaScript. For example, the keyword Some keywords are reserved, meaning that they 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: js 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:
LiteralsNote: This section discusses literals that are atomic tokens. Object literals and array literals are expressions that consist of a series of tokens. Null literalSee also js Boolean literalSee also boolean type for more information. js Numeric literalsThe Number and BigInt types use numeric literals. Decimaljs Decimal literals can start with a zero ( js ExponentialThe decimal exponential literal is specified by the following format: js BinaryBinary number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "B" ( js OctalOctal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" ( js HexadecimalHexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" ( js BigInt literalThe BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. BigInt literals are created by appending js Decimal BigInt literals cannot have leading zeros to avoid confusion with legacy octal literals. js For octal js For more information about Numeric separatorsTo improve readability for numeric literals, underscores ( js Note these limitations: js 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 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. js The following subsections describe various escape sequences ( Escape sequencesSpecial characters can be encoded using escape sequences:
The last escape sequence, js Make sure there is no space or any other character after the backslash (except for a line break), otherwise it will not work. If the next line is indented, the extra spaces will also be present in the string's value. You can also use the js Both of the above methods result in identical strings. Hexadecimal escape sequencesHexadecimal escape sequences consist of js Unicode escape sequencesA Unicode escape sequence consists of exactly four hexadecimal digits following See also js Unicode code point escapesA Unicode code point escape consists of See also js Regular expression literalsRegular expression literals are enclosed by two forward slashes ( The lexical grammar is very lenient: not all regular expression literals that get identified as one token are valid regular expressions. See also js A regular expression literal cannot start with two forward slashes ( Template literalsOne template literal consists of several tokens: See also template literals for more information. js Automatic semicolon insertionSome JavaScript statements' syntax definitions require semicolons (
However, to make the language more approachable and convenient, JavaScript is able to automatically insert semicolons when consuming the token stream, so that some invalid token sequences can be "fixed" to valid syntax. This step happens after the program text has been parsed to tokens according to the lexical grammar. 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 (including a block comment that includes at least one line terminator), or the token is "}", then a semicolon is inserted before the token. js The ending ")" of js However, semicolons are not inserted if the semicolon would then become the separator in the js Semicolons are also never inserted as empty statements. For example, in the code below, if a semicolon is inserted after ")", then the code would be valid, with an empty statement as the js 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. js 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 js Here, the js 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: js Because js This happens to be valid syntax. Within classes, class fields and generator methods can be a pitfall as well. js It is seen as: js 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:
Specifications
Browser compatibilitySee also
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |