var name = 'World!';
(function () {
if (typeof name === 'undefined') {
var name = 'Jack';
console.log('Goodbye ' + name);
} else {
console.log('Hello ' + name);
}
})();
Excuse me, name is a global variable. Why is it undefined in the immediate execution function?
Because there is also a name variable in your self-executing function. The variable name in the self-executing function is declared with var and will be promoted to the top of the scope of the self-executing function
That is, your code can be roughly viewed as executed like this
Pay attention to variable promotion, your code will become as follows after parsing