74

We're switching from MooTools to jQuery at work. I have a coworker that told me never to use $ as the prefix when using a jQuery method (if I understood him correctly). He said that instead, the more appropriate or safer way (I didn't really follow him) was to use jQuery..

So instead of $.plugin(), jQuery.plugin() should be used, for example.

Why would that be the case? What distinction is he making with $/jQuery? Should I forget about the dollar sign as my accessor? Only in certain circumstances?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
JamesBrownIsDead
  • 821
  • 1
  • 7
  • 7
  • possible duplicate of [jQuery and MooTools Conflict](http://stackoverflow.com/questions/2810399/jquery-and-mootools-conflict) – Jim G. Jan 06 '13 at 15:44
  • possible duplicate of [What is the difference between these jQuery ready functions?](http://stackoverflow.com/questions/2662778/what-is-the-difference-between-these-jquery-ready-functions) – Ciro Santilli OurBigBook.com Jun 26 '14 at 19:51

4 Answers4

87

Why would that be the case? Is his $/jQuery distinction correct?

Because almost every JavaScript library defines a function called $. When you have many libraries in one document, conflicts may appear. If you are sure that jQuery is and always will be the only script defining that function, I wouldn't have anything against using $.

jQuery defines a nice solution to resolve conflicts: jQuery.noConflict. By using this function you can define your own name, whereby jQuery will be accessible, e.g.

var $jq = jQuery.noConflict(true);

Calling this function will result in restoring previous values for $ and jQuery variables when existed before initializing jQuery. I don't remember if any other libraries try to resolve name conflicts.

If you want to use $ instead of jQuery all the time you can run your code in a separate, private scope that holds the definition of $ by using a self-invoking function.

(function($){
   // your code goes here
   $("body").append("<p>Hello World!</p>");
})(jQuery); // or even jQuery.noConflict()
Robbie JW
  • 729
  • 1
  • 9
  • 22
Rafael
  • 18,349
  • 5
  • 58
  • 67
  • 10
    Just to nitpick on the terminology: What you presented is not a closure, but just a self-invoking function. A closure is when you define a function inside of another function and return it. – Andreas Grech Feb 06 '10 at 16:49
  • 1
    My mistake. I don't know what I was thinking while writing my answer. Changed the text. – Rafael Feb 06 '10 at 20:04
  • 2
    +1 for the IIFE (immediately-invoked function expression). IMO this is preferable to using `noConflict` as the scope is clearly defined. – ach Feb 28 '14 at 17:28
  • 1
    Don't you think passing "true" in noConflict might cause problems when using plugins? I haven't tried it yet but the jQuery docs do mention that the other jQuery plugins rely on 'jQuery' variable and may not operate correctly in this situation. – PrasadW Sep 02 '15 at 06:35
11

$ is aliased to jquery and could, in theory, be aliased to something else in another included library or script which may lead to confusion or worse. If you're only using jquery, using $ should be fine.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
5

It's to avoid conflict with other libraries like Prototype that uses the same $.

I prefer the $, and there is no problem using it if you are using only Jquery.

Omar Abid
  • 15,753
  • 28
  • 77
  • 108
5

In addition to the other answers around conflicts with other libraries, a variable name should always describe the variable. jQuery is a much more descriptive name for a variable than $.

Dezza
  • 1,094
  • 4
  • 22
  • 25