Hacker News new | ask | show | jobs
by nhenezi 4319 days ago
> why is y defined? its being called out of the block it was declared in?

Javascript doesn't have block scope; above example is equivalent to:

  (function() {
    var x, y;
    x = 1;
    console.log( x );
    if ( x ) {
      y = 2;
    }
  
    console.log( y );
  })()
Variable declarations are always hoisted to the top of containing scope.