270

Is the following shorthand for $(document).ready?

(function($){

//some code

})(jQuery);

I see this pattern used a lot, but I'm unable to find any reference to it. If it is shorthand for $(document).ready(), is there any particular reason it might not work? In my tests it seems to always fire before the ready event.

2
  • Any variable defined inside the mentioned pattern function (e.g. var somevar;) will not change the contents of variables of the same name outside of the function Commented Dec 16, 2013 at 12:42
  • 3
    The code has the effect of ensuring $ represents jQuery within that function block so the code is portable to places where the $ alias for jQuery is disabled or defined as something else.
    – AsksAnyway
    Commented Mar 22, 2014 at 18:56

8 Answers 8

592

The shorthand is:

$(function() {
    // Code here
});
4
  • 28
    The first argument is $. Might want to add that in. It's useful for jQuery(function($, undefined) {});
    – Raynos
    Commented May 14, 2011 at 19:06
  • 5
    @raynos Its not required. the above code works fine as an alias for $(document).ready(function(){ }); Commented May 14, 2011 at 19:10
  • 11
    It's just useful to know that you get $ for free as the first argument.
    – Raynos
    Commented May 14, 2011 at 19:21
  • 11
    I still visit this answer once a month or so.
    – Nugsson
    Commented Nov 30, 2018 at 14:22
263

The shorthand for $(document).ready(handler) is $(handler) (where handler is a function). See here.

The code in your question has nothing to do with .ready(). Rather, it is an immediately-invoked function expression (IIFE) with the jQuery object as its argument. Its purpose is to restrict the scope of at least the $ variable to its own block so it doesn't cause conflicts. You typically see the pattern used by jQuery plugins to ensure that $ == jQuery.

1
  • 14
    Technically it's an immediately invoked function expression. Were it self-invoking, it would be calling itself from inside itself. Search the web for iife, or skip ahead to Cowboy Alman's famous blog post. Details…sheesh.
    – 2540625
    Commented Oct 22, 2014 at 19:36
94

The correct shorthand is this:

$(function() {
    // this behaves as if within document.ready
});

The code you posted…

(function($){

//some code

})(jQuery);

…creates an anonymous function and executes it immediately with jQuery being passed in as the arg $. All it effectively does is take the code inside the function and execute it like normal, since $ is already an alias for jQuery. :D

1
  • 6
    You could say that it also ensures that $ is the alias for jQuery, if other tools that also use $ as an alias are loaded
    – Jim Wolff
    Commented Jan 4, 2017 at 9:51
24

Even shorter variant is to use

$(()=>{

});

where $ stands for jQuery and ()=>{} is so called 'arrow function' that inherits this from the closure. (So that in this you'll probably have window instead of document.)

15

This is not a shorthand for $(document).ready().

The code you posted boxes the inside code and makes jQuery available as $ without polluting the global namespace. This can be used when you want to use both prototype and jQuery on one page.

Documented here: http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/#use-an-immediately-invoked-function-expression

1
  • Thank you for answering the question
    – MarcGuay
    Commented Apr 9, 2019 at 21:26
13

The multi-framework safe shorthand for ready is:

jQuery(function($, undefined) {
    // $ is guaranteed to be short for jQuery in this scope
    // undefined is provided because it could have been overwritten elsewhere
});

This is because jQuery isn't the only framework that uses the $ and undefined variables

2
  • also can be written as (function($){ ... })(jQuery); Commented Jan 20, 2015 at 5:48
  • 3
    @MikeCauser also a good approach, but it will not be called on ready, instead it's called immediately Commented Jan 20, 2015 at 7:45
11

These specific lines are the usual wrapper for jQuery plugins:

"...to make sure that your plugin doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to a self executing function (closure) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution."

(function( $ ){
  $.fn.myPlugin = function() {
    // Do your awesome plugin stuff here
  };
})( jQuery );

From http://docs.jquery.com/Plugins/Authoring

0
2

What about this?

(function($) { 
   $(function() {
     // more code using $ as alias to jQuery
     // will be fired when document is ready
   });
})(jQuery);

Not the answer you're looking for? Browse other questions tagged or ask your own question.