This article analyzes the difference between defining variables with var and without var in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:
See the instructions directly in the example:
Strictly speaking: the absence of var in the function body does not mean defining a variable, but assigning a variable value, that is, var abc;abc=8. If you assign abc=80 (without var) in the function body, the actual process is like this - the statement first searches for the variable abc in the function body. If it cannot find it, it will continue to search for the variable abc outside the function body. If Still couldn't find it. In the end, I had no choice but to define the variable var abc outside the function body.
So, why
function test(){
abc = 80;
}
This is the reason why variable abc can be called directly outside the function.
I hope this article will be helpful to everyone’s JavaScript programming design.