tags:

views:

93

answers:

6

I have seen several javascript libraries using $ be it jQuery , mootools , prototype etc also even some of the books on javascript recommend using $ as a function replacement of document.getElementById. is it just a random thing.

+1  A: 

Not completely random - many of the shell languages in unix use $ to signify a variable, and this has been the source of this "tradition".

Oded
+1  A: 

Rules for naming variables:

  1. The first character must be a letter, or an underscore (_), or a dollar sign ($).

  2. The subsequent characters can be letters, numbers, underscores, or dollar signs.

  3. The variable must not contain any embedded space characte`rs.

  4. The variable name can't be a reserved word.

So in answer to your question, it's just used as a prefix to differentiate between the library and other variables you might use.

Tom Gullen
+4  A: 

I can only assume but I think it is more "visible" than a single character or the underscore _.

Or to put it another way: It is the shortest possible identifier that stands out of the crowd.

Felix Kling
+8  A: 

The reason $ was actually introduced as a valid variable name in the ECMA specs was to distinguish machine generated code from human written code .
Most JS libraries use $ because it is very rare that you would want to name some of your variable $ and hence it would avoid collision and accidental overwriting of the variable . ( However now since every library is using $ , if you want to use two libraries , you are in a soup ) .

NM
This looks like a perfect answer to me was just thinking why different library developers didnt actually think of using something very specific for there libraries. Although appreciate how jQuery provides jQuery.noConflict to avoid accidents.
sushil bharwani
A: 

I think it is just to make it stand out from the common variable names .

jmhowwala
+1  A: 

It's a valid variable name that is short and (theoretically) wouldn't be used often. People wouldn't appreciate typing out xtremeFramework every time they wanted to use it or have it conflict with their variable i.

Casey Hope