$(function () {
 console.log('hello');
 //...
})
I saw the code like this. Don't know the purpose of adding the first line (function)? Could Someone explain this?
$(function () {
 console.log('hello');
 //...
})
I saw the code like this. Don't know the purpose of adding the first line (function)? Could Someone explain this?
 
    
     
    
    Wrapping you Javascript code with $(function () {:
$(function () {
    console.log( "ready!" );
});
is the same (a Shorthand version) as writing:
$(document).ready(function() {
    console.log( "ready!" );
});
Which ensures the script will only run once the page Document Object Model (DOM) is ready for Javascript code to execute (Jquery docs).
