Diego
Diego

Reputation: 16714

$ vs. jQuery: Which should I use?

What is the diffrence between them? Which is better?

Upvotes: 7

Views: 274

Answers (3)

thegryphon
thegryphon

Reputation: 134

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)

Upvotes: 1

Ron Potato
Ron Potato

Reputation: 11

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/

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630379

$ 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);

Upvotes: 15

Related Questions