Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Disclaimer: I know there is no strictly correct answer to this, but I've come across something that does not make sense to me, and I'd like to try to understand it better by seeing what other people have to say on the subject.

Anyways, I was thinking about how many different JavaScript frameworks all define slightly different variations of a $() function (and also things like $$(), $E(), and so on), and was wondering why they do it. This is basically what I came up with:


  • Typing $() is fast.


  • The name $ does not imply anything semantically meaningful by itself.
  • $ is a property in the global namespace, and thus can only be claimed by a single framework at any given time.
  • Having multiple frameworks competing over the same global name(s) makes them less easily compatible with each other.
  • Older code that uses the $() function defined in one framework can be accidentally broken by simply importing a second framework that provides its own version of $().
  • With everybody providing a different version of $(), code becomes less readable because one cannot make assumptions about what $() is doing without knowing what framework is in use.
  • Sometimes novice developers get the wrong idea from all the dollar signs flying around, and think that they need to prefix all their function declarations with "$".

So it seems to me that the cons far outweigh the pros. Which begs the question, if there are so many negatives associated with claiming $() as one's own, why do many frameworks do this automatically? Are there some other benefits that I am missing?

share|improve this question

closed as not constructive by Jarrod Roberson, ChrisF Mar 1 '12 at 22:27

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

jQuery has the noConflict method, to allow you to avoid conflicts over $ when using multiple frameworks. Not sure whether other frameworks have similar functions. –  Damien_The_Unbeliever Jul 16 '11 at 11:33
I would argue that it's fairly obvious what framework is by what the argument of $ is in any given context. For example, if I see $('div.test'), I'm fairly confident that I'm working with jQuery; on the other hand, if I see $('some_id') fairly I'm confident that I'm working with either MooTools or Prototype. You're right that you can't be sure of what $ does unless you know what framework is in use, but if you look at existing code, it's not really an issue. –  Reid Jul 16 '11 at 18:51
If you're using multiple frameworks together, ie jQuery and MooTools, you're doing something wrong... –  VirtuosiMedia Aug 2 '11 at 4:30
@VirtuosiMedia - Why is that? Are you saying that it is not ever possible for one framework to do something that another framework does not, or that there is no case where having a combined feature-set is desirable? For instance, on webcomix I used MooTools (with some custom z-indexing modifications) to implement draggable divs. But the social toolbar plugin uses jQuery, which caused integration conflicts. Is your assertion that I should reimplement the draggable divs in jQuery just because I want to use a social toolbar plugin that uses jQuery? –  aroth Aug 2 '11 at 6:07
I'm sure each framework has some features that others don't, but they're all based on JavaScript and for the most part, all of the bigger frameworks will have a lot of similar functionality and plugins. Rather than loading two full frameworks into your page, I would always recommend using only one and searching for a similar plugin or porting an existing plugin from one framework to another. For example, check out MooSocialize: artviper.net/website-tools/… or jQuery UI's jqueryui.com/demos/draggable –  VirtuosiMedia Aug 2 '11 at 7:38

6 Answers 6

Being fast to type is one positive. Being fast to type yet easy to spot is even better -- and people tend to spot dollar signs in walls of text relatively quickly.

share|improve this answer
Being very concise and unlikely to conflict with pre-existing variables in pre-existing code is even better too. $ is just as valid syntactically as X or i but before the frameworks almost nobody used it. –  SF. Aug 1 '11 at 21:48

Although it may seem like the cons outweigh the pros, that's not really the case for the majority. $, while conflicting with other libraries is far less likely to conflict with pre-written or future global variable names written by the end developer. Adding that to your other pro, that it's short and fast to type — and remembering that framework "selector" functions are used more frequently than most other functions — for most users, these pros far outweigh the seemingly insignificant cons you posted.

Another character could be used. For instance, Underscore uses... you guessed it, _. But most other allowable characters can't be easily input from a keyboard without using awkward alt combinations that are even less accessible on laptops.

share|improve this answer

You pretty much nailed it: Typing $() is fast.

And that's about it, with all the cons you enumerated (and possibly some more).

I work with ExtJS mostly, and this framework did not give in to this fad. It tries to keep all it's components in dedicated namespaces (in ExtJS 4 totally avoiding polluting the global namespace)

share|improve this answer

The name $ does not imply anything semantically meaningful by itself.

Which is exactly the point - when using $ as an element selector (the most common use), the function itself is not really important; its return value is, as that's what will usually be operated on by a chained function call.

The choice of $ could be due to its use as variable marker in shell scripts, Perl and PHP.

share|improve this answer

I expect that they wanted something that was visually noticeable, short and generally unused. As such there are two obvious choices that are valid javascript identifiers: "$" and "_".

I tend to think of the jQuery $ as an operator not a function in terms of how I use it.

share|improve this answer

You can simply use jQuery instead of $. Why they all agree, I don't know.

I believe it's a matter of history with namespaces.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.