We know that in programming languages, scopes are about controlling variables, the visible range of parameters, and the life cycle.
JS provides function scope functions, such as variables defined in the function outside of the variable is inaccessible:
function Jsfunc () { var a = 5; // undefined
But JS does not provide the function of block scope, such as we in the Java code if the following code:
Public Static false ; Public Static void Main (string[] args) { if(! b) { int a = 5; } // compile-time error is reported here }
When printing output A, the variable does not define an exception.
And in JS, write the following code:
var false ; function Jsfunc () {
alert (a); Undefined if(! FLG) { var a = 5; } Alert (a// 5}jsfunc ();
alert (a); Uncaught referenceerror:a is not defined
When it pops up 5, it shows that variables defined within the block-level elements within the function can be accessed within the scope of the entire function.
Therefore, we need to pay extra attention when naming variables to avoid the problem of parameter punch.