javascript – Identifiers in ECMAScript

Question:

What are the allowed characters, or what is allowed, within a name (known as an "identifier") in ECMAScript 6 ?

Do you have rules between identifiers and keywords???

Answer:

allowed characters

What characters are allowed within an identifier on ES6?

To begin with, the name of an identifier ( IdentifierName , which gives rise to the Identifier ) is specified in this grammar:

IdentifierName ::
   * IdentifierStart
   * IdentifierName IdentifierPart
IdentifierStart ::
   * UnicodeIDStart
   * $
   * _
   * \ UnicodeEscapeSequence
IdentifierPart ::
   * UnicodeIDContinue
   * $
   * _
   * \ UnicodeEscapeSequence
   * <ZWNJ>
   * <ZWJ>
UnicodeIDStart ::
   * qualquer caractere na categoria “ID_Start”
UnicodeIDContinue ::
   * qualquer caractere na categoria “ID_Continue”

IdentifierName clearly means name that starts with IdentifierStart and continues with 0 or more IdentifierPart s . It's recursive, but we can think of it that way.

Basically allowed characters are '$' and '_' . Especially also the UnicodeEscapeSequence lexical element, those \uhhhh and \u{hex} .

The IdentifierStart and IdentifierPart elements support more than one Unicode category, at least on ES 3 . On ES6 IdentifierStart accepts all characters from the ID_Start category, while IdentifierPart accepts all from ID_Continue and 2 inclusive characters ( U+200C and U+200D ).

Tip

See, Unicode Utilities: UnicodeSet . This site evaluates a pattern and displays a list of equivalent characters ( set ).

This pattern…

[:age=5.1:]&[[:ID_Start:]]

results in the characters of the ID_Start category in Unicode version 5.1. To include an abbreviated category like Pc , use something like [:gc=Pc:]

Notices about the cited utility

  • Some characters may not appear in the top result, perhaps because of the browser. To make sure all characters appear, enable the Escape option so that non-ASCII characters appear correctly. Non-basic characters will appear with \UHHHHHHHH , while the rest will appear normal or in the form \uHHHH . Modify the result as you wish.

  • So far the utility has correctly arranged descending to ascending order in the sets results for me, on the subject of identifiers. With categories like Zs (certain line breaks) it didn't organize it right, so maybe you might need to sort it manually, or using a tool. It's not that hard to make one.

Some rules

UnicodeEscapeSequence Rule

  • A UnicodeEscapeSequence must contribute a character allowed in the IdentifierStart as well as the IdentifierPart . This is simply because this escape can result in any character.

reserved identifiers

We all know that keywords ( ReservedWord ) must appear in the right context. Some of these keywords are future-reserved ( FutureReservedWord ).

Any piece of code contained in strict mode contains more keywords in the lexical element FutureReservedWord . See section § 11.6.2.2 Future Reserved Words .

And in the small section § 12.1.1 Static Semantics: Early Errors are some reserved identifier sticks.

Especially, some interpreters restrict the appearance of UnicodeEscapeSequence s within keywords (eg instanceo\u{66} ) and make exceptions. Some interpreters (like the ActionScript 3 used in mxmlc probably) can ignore keywords with UnicodeEscape s, but that means it was poorly implemented.

Scroll to Top