(function() {
...
...
}());
this is just a way to declare an anonymous function and call it
var a = 'g';
function foo() {
console.log(a);
a = 7; //
}
console.log(foo(), a);
here u are declaring a variable a = 'g' in javascript if u declare a variable using the keyword "var" the scope of that variable si the function, that means that inside that function (also in sub function) everyone can use it.
then u are declaring a foo function, the foo function can see the variable a because foo is declared inside the previous function.
console.log(a) just print the value of a so it will print 'g' and then change the value of a to 7.
after the declaration u call console.log(foo(), a), this means that u are executing foo and printing the return value of foo that is 'undefined' because there is no return statement, and then printing the value of a that after the execution of foo is become 7.
so the output is:
g
undefined 7
foowhich is whyconsole.log(foo())returns undefined