Is my assumptions are correct about following code:
Here is the code:
1 var foo = 1;
2 if (true) {
3 var foo = 10;
4 }
5 console.log(foo);
var on line 3 and 1 is hoisted to top and code becomes:
1 var foo;
2 foo = 1;
3 if (true) {
4 foo = 10;
5 }
6 console.log(foo);
Is this understanding right? Is this how hoisting work in JS?
Actually first I hoisted var on line 3 but then I had to hoist the one which was on line 1, cause it was declaration and was no longer on the top.