0

The following code snippet logs undefined. Could someone please explain why?

var x = 100;

function test() {
  if (false) {
    var x = 199;
  }
  console.log(x);
}
test();
connexo
  • 53,704
  • 14
  • 91
  • 128
Vipin Verma
  • 5,330
  • 11
  • 50
  • 92
  • 2
    The line that assigns to `x` inside `test` never runs... – CertainPerformance Jan 22 '19 at 23:47
  • https://developer.mozilla.org/en-US/docs/Glossary/Hoisting ... `var` declarations are function scoped, not block scoped. – Felix Kling Jan 22 '19 at 23:47
  • 1
    @CertainPerformance That's the whole point of the question. Despite being never run, the declaration is hoisted, creating a function scoped variable `x` that is never assigned a value. – connexo Jan 23 '19 at 00:01

0 Answers0