7

What is the diffrence between them? Which is better?

Diego
  • 16,026
  • 24
  • 80
  • 135
  • possible duplicate of [Jquery: The $ dollarsign](http://stackoverflow.com/questions/1180213/jquery-the-dollarsign) – Russ Cam Dec 24 '10 at 14:16

3 Answers3

15

$ is an alias for jQuery, neither is "better" really, jQuery is provided in case something else is using $, like Prototype (or jQuery.noConflict() was called for another reason...).

I prefer $ for brevity because I know it refers to jQuery, if you're unsure (like when writing a plugin) use jQuery for your primary reference, for example:

(function($) {
  //inside here $ means jQuery
})(jQuery);
Nick Craver
  • 610,884
  • 134
  • 1,288
  • 1,151
  • 1
    So, without prototype is it the same? – Diego Dec 24 '10 at 14:05
  • 2
    @Diego - yup, unless you call `jQuery.noConflict()`, which releases `window.$` and sets it to whatever it was before. – Nick Craver Dec 24 '10 at 14:07
  • @Diego - They reference the same function so long as nothing else has overwritten `$` (i.e. `window.$`) following load and execution of the jQuery script – Russ Cam Dec 24 '10 at 14:08
1

The functionality is identical if there is no conflict.

Use 'jQuery' instead of '$' to be especially explicit/descriptive, or if you currently use or anticipate using another library that defines '$'.

See also http://api.jquery.com/jQuery.noConflict/

Ron Potato
  • 11
  • 1
1

jQuery == $ == window.jQuery == window.$

jQuery and $ are defined in window, and $ can be used if no other library is making use of it, thus creating conflicts.

Either use jQuery.noConflict() or closures:

(function ($) {
    // code with $ here
})(jQuery)
Andrew Whitaker
  • 121,982
  • 31
  • 284
  • 303
thegryphon
  • 114
  • 2