0

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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user4904589
  • 547
  • 2
  • 5
  • 15
  • 2
    Yes, this is how hoisting works. Since you know the term "hoisting" a simple search for "Javascript hoisting" would have given you tons of articles and answers here on StackOverflow to read as this is well discussed topic. I will now find a duplicate here on StackOverflow to mark this. – jfriend00 Sep 19 '15 at 06:22
  • I think CodeSchool's JavaScript Road Trip covers hoisting quite well, but I don't remember which part. – Karl Wilbur Sep 19 '15 at 06:38

0 Answers0