Dollar sign before self declaring anonymous function in JavaScript?

xil3 picture xil3 · Sep 30, 2011 · Viewed 8.4k times · Source

What is the difference between these two:

$(function () {
    // do stuff
});

AND

(function () {
    // do stuff
})();

Answer

g.d.d.c picture g.d.d.c · Sep 30, 2011

The first uses jQuery to bind a function to the document.ready event. The second declares and immediately executes a function.