1

The below function outputs undefined.

var x = 100;
function test() {
  if(false){
    var x = 130;
  }
  console.log(x);
}

test();

I was with the perception that, since the statement inside IF block is not executed X should display 100 since X value is declared globally.

If I comment the IF block then the global X value is taken.

Could someone explain the reason behind this.

user804401
  • 1,990
  • 9
  • 38
  • 71
  • This would be due to Javascripts hoisting, you can read more about it [here](https://blog.wax-o.com/2014/07/how-and-why-the-hoisting-works-in-javascript/) – Snel23 May 14 '19 at 02:17

0 Answers0