0

I read some where else that following is illegal

var 3po = true;

but

var highNoon = false;

is legal.

Could someone explain what is all this means? Why the first statement is illegal while the second is legal?

Peter
  • 1,443
  • 4
  • 18
  • 36

4 Answers4

7

An identifier cannot start with a number as the ECMAScript grammar explains:

Identifier :: 
    IdentifierName but not ReservedWord 

IdentifierName :: 
    IdentifierStart 
    IdentifierName IdentifierPart 

IdentifierStart :: 
    UnicodeLetter 
    $ 
    _ 
    \ UnicodeEscapeSequence 

IdentifierPart :: 
    IdentifierStart 
    UnicodeCombiningMark 
    UnicodeDigit 
    UnicodeConnectorPunctuation 
    <ZWNJ> 
    <ZWJ>
ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
2

I think you mean legal as in Syntax-wise?

The first example will give you a SyntaxError, the reason is that variables should not begin with a number.

MarioRicalde
  • 8,575
  • 6
  • 38
  • 41
0

You can find the answer in the EcmaScript documentation, page 15.

IdentifierName ::
    IdentifierStart
    IdentifierName IdentifierPart

IdentifierStart ::
    UnicodeLetter
    $
    _
    \ UnicodeEscapeSequence

So, the identifier could start from the letter, $, _ or unicode escape sequence, not the digit.

bjornd
  • 21,619
  • 4
  • 53
  • 70
-1

Is it possible that you meant "Why variables should not begin with a number?"

Just guesing...

pencilCake
  • 48,449
  • 79
  • 219
  • 356