Difference between the two functions:
(function($){
console.log($);
}("test"));
(function($){
console.log($);
})("test");
I’ve tried it out on the web console and pressed ‘Run’. They return the same thing. What exactly is the difference with the change of parenthesis location?
I am assuming that the second one is run immediately, right?
The two snippets are identical; neither function returns a value and both log their argument to the console. Similarly, both anonymous functions are called with the argument
"test".The only difference is in syntax, regarding the grouping of the function definition vs its application to its arguments. The first function groups the function definition along with the
()operator, whereas the second function groups the function definition separately from its application.Ultimately there is no semantic difference. Both snippets contain an anonymous function which logs its argument and is then called with the argument “test”.