$(document).ready(function() VS $(function(){ [duplicate]

The two ways are equivalent, I personally prefer the second, $(function() {}); it’s just a shortcut for document ready.

About the new jQuery(document)... construct, you don’t really need to use the new operator, jQuery will use it internally if you don’t.

The argument that the ready handler function receives, is the jQuery object itself.

That’s quite useful where you have to run jQuery in compatibility mode with other libraries, for example:

jQuery(function ($) {
  // use $ here
});

The $ argument inside the callback will refer to the jQuery object, outside that function it might refer to another library like PrototypeJS.

Leave a Comment