This is the 20th day of my participation in the August More Text Challenge

console.log(a);
console.log(b);
var a = 12;
let b = 13;
if(1==1){
    console.log(a);
    console.log(b);
    var a = 100;
    let b = 200;
    console.log(a);
    console.log(b)
}
console.log(a);
console.log(b)
Copy the code

knowledge

  • In addition toFunctions and objectsOutside of the braces [e.g., true questions, loop bodies, code blocks… If it appears in braceslet/const /function/classThe current curly brace produces a private context whose parent context is the context in which it is located.varDoes not generate and is not affected by block-level context

After learning the knowledge point, we will analyze this example in detail. Ow, hey, do what we learn to apply… The premise is that we do not consider the error code and do not continue to execute this general premise of analysis. Var /function will be promoted, var will not be assigned, so a will be defined globally, because var is not affected by block level context, so global promotion will only be a variable and then it will be pushed to execute the code, first console.log(a); For undefined, we analyze the code there is a major premise, then the console. The log (b) the results for the error, code execution, a global variable has a value of 12, b value of 13 under the global, next is the block-level scope, no variable, print the console. The log (a) the value of 12 for the global, Console. log(b) : error: var a = 100; var a = 100; var a = 100; Console. log(a) is 100, console.log(b) is 200, and a is 100, b is 13