I was researching on jQuery best practices and found this article by Greg Franko
Normally, I do:
$(\"document\").ready(function() {
// The DOM i
If you use $ as an alias for jQuery then
$(document).ready(function(){})
is same as
(function($, window, document) {
// The $ is now locally scoped
// Listen for the jQuery ready event on the document
$(function() {
// The DOM is ready!
});
// The rest of the code goes here!
}(window.jQuery, window, document));
As pointed out in an earlier answer, the second method insulates you from using $ alias freely for jQuery as it passes the jQuery object to the Immediately invoked function expression, which basically keeps the variables and code within it private, and does not pollute the global namespace.
In short, if you resort to the first method and use other libraries using $, you will end with conflict.