What is difference between following code?
Code 1:
var f = function() {
// function body code
// ...
// ...
}();
Code 2:
var f = (function(){
// function body code
// ...
// ...
})();
Which one is better to use?
What is difference between following code?
Code 1:
var f = function() {
// function body code
// ...
// ...
}();
Code 2:
var f = (function(){
// function body code
// ...
// ...
})();
Which one is better to use?
They're exactly equivalent given the syntax of the language.
Since it would be invalid syntax without the assignment, as in
function() {
// ...
}();
it may be clearer to always wrap anonymous functions in parentheses.