5

Possible Duplicates:
Can someone explain the dollar sign in Javascript?
Why would a javascript variable start with a dollar sign?

Why is it that I can assign a function to $ in Javascript, but not # or ^ ?

Community
  • 1
  • 1
Mark
  • 39,169
  • 11
  • 42
  • 48
  • There is other mining of `^` in JS. – Stan Kurilin Jan 25 '11 at 15:48
  • 3
    Because the grammar says so. (Not what you wanted to hear? Then be more specific.) –  Jan 25 '11 at 15:50
  • 2
    I would suggest that this is not an *exact* duplicate of the other questions. **edit to add** The other questions are asking why you would *use* the `$` char, this question is asking why `$` is ok vs `#` and `^`. – zzzzBov Jan 25 '11 at 16:00

4 Answers4

11

From the ECMA standard (Section 7.6)

The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
1

Because that is what ECMA-262 specifies (see section 7.6)

Identifiers must match this RegEx: [a-zA-Z_$][0-9a-zA-Z_$]*

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
1

The reason is because JavaScript is part of the ECMA-262 standard.

If you read section 7.6 you'll see the part about Identifier syntax.

Essentially the characters that can be used are defined by:

Identifier ::
  IdentifierName but not ReservedWord

IdentifierName ::
  IdentifierStart
  IdentifierName IdentifierPart

IdentifierStart ::
  UnicodeLetter
  $
  _
  \
  UnicodeEscapeSequence

IdentifierPart ::
  IdentifierStart
  UnicodeCombiningMark
  UnicodeDigit
  UnicodeConnectorPunctuation
  <ZWNJ>
  <ZWJ>
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

If I understand your question, it's simply because the Javascript interpreter ignores $ as any type of special character. You can assign a function to a and you can assign one to $.

Much like an underscore, it treats $ as any "normal" character.

user535617
  • 634
  • 2
  • 7
  • 22