What is this syntax? ; (function ($, undefined)

javascript, jquery

Solution

(function ($, undefined)
{

    // all the variables and functions of the js document

})(jQuery);

calls a block of code ensuring that inside

- $ is usable to refer to `jQuery`

- `undefined` is `undefined` (edit: this was useful because `undefined` could be redefined at that time in the oldest browsers, it's now useless)

and that any minifier can change `undefined` to a shorter label.

The initial `;` ensures you can concatenate this file with another one : without this, you'd have an error executing the concatened file if the one just before was something like

(function (){

})()

Problem

``` ; (function ($, undefined) { // all the variables and functions of the js document })(jQuery); ``` I've seen this twice now in the jquery/javascript files for a zoom script. I don't understand what this is exactly. I can't seem to google it, I don't remember coming across this on tizag or w3schools while recently learning jquery and js. There's nothing before or after this code (other than some comments). So I'm utterly lost as to what `(function())(jQuery);` is or does.

Original source

Related problems