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,481
  • 4
  • 19
  • 37

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
  • 310,957
  • 84
  • 592
  • 636
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
  • 9,131
  • 6
  • 40
  • 42
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
  • 22,397
  • 4
  • 57
  • 73
-1

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

Just guesing...

pencilCake
  • 51,323
  • 85
  • 226
  • 363