0

I've noticed some functions in Javascript are contained within $(). For example:

$(function() {
     //do something here
});

As opposed to simply:

function() {
     //do something here
};

I know this notation isn't needed for a Javascript function to work, so what does the $() actually do?

Single Entity
  • 2,925
  • 3
  • 37
  • 66

1 Answers1

1

It's Jquery's short hand for $(document).ready(function(){});

https://learn.jquery.com/using-jquery-core/document-ready/

By the way, your second function will never be executed. You need to add () if you want it to be executed.

function() {
     //do something here
}();
Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66