1. The external one is global, and the internal one is local variable.
2. Add var as a local variable (in the method), and do not add var as a global variable (after it is used once in the method)
The code copy is as follows:
<script type="text/javascript">
var golbe="global";
test();
function test(){
var local="local";
document.write(golbe);
document.write(local);
}
document.write(golbe);
document.write(local);
</script>
In the above test method, when the var of the local variable is removed, local becomes a global variable, but if local is not used locally, this local is invalid as global.
In order to verify this, I commented out the only code inside the test method using local variable. I found that it could not be printed outside.
Summary: Global variables can not declare var. Var must be declared. It has no effect when defining global variables. Adding or not adding var keywords has no effect; but when defining local variables, if the var keyword is not added, the javascript interpreter will interpret it as a global variable.