When accessing object["1foo"]
you are not creating an identifier but a key to look up a value inside your object.
Identifiers cannot begin with a digit, keys don't have any such specifications (of kind of "obvious" reasons.
Identifiers are not values, keys (in the sense described in this post) are not identifiers.
When doing {"1foo": 123}
you are not making 1foo
a proper identifier, it's a rvalue variable of type string
.
According to the ECMA standard
7.6 Identifier Names and Identifiers
Identifier ::
- IdentifierName but not ReservedWord
IdentifierName ::
- IdentifierStart
- IdentifierName, IdentifierPart
IdentifierStart ::
- UnicodeLetter
- $
- _
- \ UnicodeEscapeSequence
IdentifierPart ::
- IdentifierStart
- UnicodeCombiningMark
- UnicodeDigit
- UnicodeConnectorPunctuation
- <ZWNJ>
- <ZWJ>
...
11.1.5 Object Initializer
PropertyName [can be any of the following]:
- IdentifierName
- StringLiteral
- NumericLiteral