Native JS judge scope output value

Time:2021-2-21

JS engine running

First parsing (also known as pre parsing): variable promotion (includingFunction expression)And function promotion

one ️⃣ The premise is thatScopeFunction or script
2. Only promote the variable name to the top of the current scope, and do not assign or call
three ️⃣ Check for syntax errors (none if any)

Second parsing: code running

From top to bottom, check for running errors (if any, do not go down)

 

Scope exercises

Please describe the output of the following code and the reason for running it

(Scope List)

Question 1

console.log(a);
var a = 10;
console.log(a);
var a = 20;
console.log(a);
b();
var b = function() {
    console.log(a);
}

A solution

/*It is equivalent to executing the following code*/
var a;
console.log (a) ; // a no assignment, undefined
a = 10;
var a;
console.log(a); // 10
a = 20;
console.log(a); // 20
var b;
B (); // call directly without assignment. B is not a function
b = function() {
    console.log(a);
}

Question 2

var num = 10;
fun();
function fun() {
    console.log(num);
    var num = 20;
}

Second solution

/*It is equivalent to performing the following operations*/
var num;
function fun() {
    var num;
    console.log(num);  // undefined
    num = 20;
}
num = 10;
fun();

Question 3

var num = 10;

function fn() {
    console.log(num);  
    var num = 20;
    console.log(num);  
}
fn();

Three solutions

/*Equivalent to the following code*/
var num;
function fn() {
    var num;
    console.log(num);  // underfined
    num = 20;
    console.log(num);  // 20
}
num = 10;
fn();

Question 4

var a = 18;
f1();
function f1() {
    var b = 9;
    console.log(a);
    console.log(b);
    var a = '123';
}

Four solutions

/*Equivalent to the following code*/
var a;
function f1() {
    var b;
    var a;
    b = 9;
    console.log(a);  // undefined
    console.log(b);  // 9
    a = '123';
}
a = 18;
f1();

Question 5

f1()
console.log(c);
console.log(b);
console.log(a);
function f1() {
    var a = b = c = 9; 
    console.log(a); 
    console.log(b); 
    console.log(c); 
}

Five solutions

/*It is equivalent to executing the following code*/
b=9;
c=9;
function f1() {
    Var a = 9; // distinguish collectively declared variables var a = 9, B = 9, C = 9;
    B = 9; // if the function scope is silent, then B and C are global variables
    c = 9;
    console.log(a);  // 9
    console.log(b);  // 9
    console.log(c);  // 9
}
f1();
console.log(c);  // 9
console.log(b);  // 9
console.log (a) ; // error: A is not defined

Question 6

var num = 1;
function demo() {
    console.log(num); 
    function test() {
        console.log(num); 
        num = 3;
        console.log(num); 
    }
    var num = 2
    test();
}
demo();

Six solutions

/*It is equivalent to executing the following code*/
var num;
function demo() {
    var num;
    console.log(num); // underfined
    function test() {
        console.log(num); // 2
        num = 3;
        console.log(num); // 3
    }
    num = 2;
    test();
}
num = 1;
demo();