I came across this code:
var myVar = 'foo';
(function() {
console.log('Original value was: ' + myVar);
var myVar = 'bar';
console.log('New value is: ' + myVar);
})();
Questions:
myVar
?
1a. IF it is, is it executed before global myVar
is declared?undefined
first, and bar
second. What is the behind the scenes order of execution in IIFE?var myVar
inside the IIFE is hoisted to the top of the function scope, but the assignment is not. The following is equivalent:(function(){
var myVar;
console.log('Original value was: '+ myVar);
myVar = 'bar';
console.log('New value is: ' + myVar);
})();