Confused about hoisting

前端 未结 5 1485
感情败类
感情败类 2021-01-21 03:09

consider these slightly two different versions of hoisting...

mylocation = \"dublin\" 
function outputPosition() {
    alert(mylocation);
    mylocation = \"fing         


        
5条回答
  •  独厮守ぢ
    2021-01-21 03:47

    Take for example :

    x = 5;
    var x;
    
    console.log( x );
    

    Technically you might want to look at x = 5; as a wrong statement in this context considering the fact that it comes before the declaration, but the JS Engine doesn't work that way. It sees x = 5 and var x as two separate statements, the first one a compiler-related task, and the second one an execution-related task.

    what this means in simple terms is that all declarations in a scope, regardless of where they appear, are processed first before the code itself is executed. i.e you can execute a variable before declaring it.

提交回复
热议问题