One of the thinks which I learned recently is how function declaration and function expression are hoisted. I wrote this post to not forget. Enjoy and sorry for potential errors!
As a quick reminder
** Function Declaration:
** Function Expression:
Anonymous function expression
Named function expression
What gets returned in this case?
If you said 8 congratulations! If not, I’ll try to quickly explain why it’s not the case.
The behaviour above is caused by hoisting. What that means is that before the code is ‘run’, all variables and apparently function declarations are hoisted to the top of our code. So this code during runtime would look like this:
So the way it works, and testMe() is able to reach 8 is because before the return statements is reached the second statement which returns 8 is ‘lifted’ above the return statement and overwrites the first one which returns 3. It’s the same as in this case:
What about Function Expression?
The result here is 3. Why? As mentioned before variables are hoisted, and here we’ve got variables, don’t we?
Let’s have a look how all get’s sorted by the javascript engine before testMe() get’s called.
In this case two variables got hoisted to the top of the scope, then a function has been assigned to it and we returned that value.
The article which motivated me to write my own to better remember ‘this’ hoisting stuff is here: https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/