(function() {
var a = b = 3;
})();
console.log(a, b);
(function() {
var x = y = 3;
console.log(x, y);
})();
These are very simple program. But I'm confused why a
is undefined while b
has value 3 in the output of the first example program while both x
and y
are having the value 3 in the second example program?
(function() {
var a = b = 3;
})();
console.log(a, b);
When a IIFE is created , a function is created and is called.Here a is the local variable and variables have local scope so you cant access it outside
You havent used var in front of b so it becomes the global variable and hence is reflected outside the function scope too
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