There is no block-level scope in the
javascript, and instead of JavaScript using function scopes, use the example below to learn how to use JS scopes
In some programming languages like C, each piece of code within the curly braces has its own scope, and the variables are invisible beyond the declaration of their code snippet, which we call block-level scopes, and no block-level scopes in JavaScript. Instead, JavaScript uses function scopes: variables are defined in the body of the function that declares it and in any function that is nested within the function body. In the following code, I,J and K, defined in different locations, are defined in the same scope code as follows: function text (o) { var i=0; &nb sp; alert (typeof O); if (typeof o = "string") { var j=0; for (var k=0;k<10;k++) { alert (k);/output 0-9 } alert (k);/output } alert (j);/Output 0 } The function scope of JavaScript refers to all variables declared within a function that are always visible within the function body. Interestingly, this means that the variable is even available before it is declared. This feature of JavaScript is informally called Declaration advance (hoisting), which is that all variables declared in the function body of JavaScript (not involving assignment) are "advanced" to the top of the function body. See ToThe following code code is as follows: Var global= "Globas"; function Globals () { alert (global)//undefined & nbsp var global= "Hello Qdao"; Alert (global)//hello Qdao } Because of the nature of the function scope, local variables are always defined throughout the function body, That is, the variable inside the function body obscures the global variable with the same name. However, when the program executes to the VAR statement, local variables are actually assigned, so the procedure above is equivalent to declaring the variable in the function "ahead" to the top of the function body, and leaving the colleague variable initialization at the original location: Code as follows: Var global= " Globas "; function Globals { var global; &NB Sp Alert (global);//undefined global= "Hello Qdao"; Alert (global)//hello Qdao }