(function() {
var a = 'g';
function foo() {
console.log(a);
a = 7; //
}
console.log(foo(), a);
}());
Can anybody explain the step by step execution and out put of this code sample. I got the out put as 'g undefined 7'
You are creating an anonymous function and executing right away (IIFE)
Then, in this scope function :
a var with value ga var.
This foofunction is not called right away.logstatement, foo is executed :
a is logged (value g)a var in IIFE scope is changed to value 7foo returns nothingfoo returns value which is undefined (no return value)a value, which is 7 after foo is executed.So you have in your console :
g
undefined 7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With