An interview question that gets 90% of the answers wrong

Without further ado, to the point.

var a = 1;
{
    a = 2;
    function a() {}
    a = 3;
    console.log(a);
}
console.log(a);
Copy the code

How’s it going? Did you get it?

Leave your thoughts in the comments section.


// "use strict";
var a = 1; 
console.log(a, window.a); // 1  1
{
    console.log(a, window.a); // f a(){} 1
    a = 2; 
    console.log(a, window.a); 1 / / 2
    function a() {}
    console.log(a, window.a); 2 / / 2
    a = 3;
    console.log(a, window.a); 2 / / 3
}
console.log(a, window.a); 2 / / 2
Copy the code
"use strict";
var a = 1; // globally declare a variable a
console.log(a, window.a); // 1  1
{
    console.log(a, window.a); // f a(){} 1
    a = 2; // Find the function a declared in the block-level scope and assign it to 2.
    console.log(a, window.a); 1 / / 2
    function a() {}
    console.log(a, window.a); 1 / / 2
    a = 3;
    console.log(a, window.a); / 1/3
}
console.log(a, window.a); // 1  1
Copy the code