Alright, here we have a very simple script that first declares a global variable called myGlobal followed by a hoistMe method which contains its own declaration of the myGlobal variable and a series of alert messages. Let’s run this and see what happens.
Let’s take a look at an example using function statements:
Now, when you first look at this, you’d think that your alert would say 5 when in fact you get 15:
In the above example, the declaration of foo that returns 15 is overriding the declaration of foo that returns 5.
I hope this helps someone out there. If you have any questions, please leave a comment below.