One of the quirkiest things about JavaScript is this thing known as hoisting. We'll get to what it means in a bit, but let's set the stage for it by looking at some examples and figuring out what the right behavior should be. For our first example, take a look at the following code:
The hoisting mechanics you describe are notional — “as if” — behavior. Also, hoisting happens because it’s in the language spec, not because it’s an optimization. It’s mostly a vestigial remnant of early versions of JS.