Typical examples of var promotion in javascript

// I just read a little question in the book, which is very interesting. Let’s think about it together
myname = 'global';
var fn = function () {
	console.log(myname); // undefined
	var myname = 'local';
	console.log(myname); // local
};
fn()

// The above code is actually equivalent to the following code
myname = 'global';
var fn = function () {
	var myname ;
	console.log(myname); // undefined
	myname = 'local';
	console.log(myname); // local
};

 

Guess you like

Origin blog.csdn.net/alxw2010/article/details/84617803