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 ^ ?

asked Jan 25, 2011 at 15:46
3
  • There is other mining of ^ in JS. Commented Jan 25, 2011 at 15:48
  • 3
    Because the grammar says so. (Not what you wanted to hear? Then be more specific.) Commented Jan 25, 2011 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 ^. Commented Jan 25, 2011 at 16:00

4 Answers 4

11

From the ECMA standard (Section 7.6)

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

answered Jan 25, 2011 at 15:52
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that's what I thought. Thank you!
1

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

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

answered Jan 25, 2011 at 15:52

Comments

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>
answered Jan 25, 2011 at 15:57

Comments

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.

answered Jan 25, 2011 at 15:49

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.