Javascript Odd Scoping Behavior

后端 未结 3 621
Happy的楠姐
Happy的楠姐 2021-01-22 07:21

I\'ve been going through Javascript function scope and have run into this:

var scope = \"global\";

function f(){
    console.log(scope);

    var scope = \"loca         


        
3条回答
  •  一整个雨季
    2021-01-22 08:02

    If you omit the var statement, the first log uses the global variable, which is set with the string "global". There is no other local variable and no hoisting.

    1. First log: global variable scope set with "global" content
    2. Assignment of new string for the same global variable
    3. Second log: global variable scope set with "local" content

提交回复
热议问题