why does several javascript libraries use $ for one or other use

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.



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 ) .


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.


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


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.


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.


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


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.