In JavaScript, an area is tied to closure (the last function block included or by default refers to a window object). When a variable is declared somewhere inside this function block, it is raised at the top of the scope, so essentially the variable exists as undefined, starting at the very top of the scope if it is declared anywhere in the scope.
Think about it as soon as the code starts executing, it scans all the instructions for the declarations and immediately sends the symbol name.
console.log(x); // undefined console.log(y); // error: Uncaught ReferenceError: y is not defined var x;
In this case, you can do this to the extreme:
console.log(x); // undefined, not an error while (false) { if (false) { var x; } }
even if var x cannot be reached, and at run time will be fully optimized. the motor will raise a variable at the top of the area
hope this helps -ck
useful link: http://www.youtube.com/watch?v=taaEzHI9xyY&feature=youtu.be#t=42m57s
source share