Tuesday, December 6, 2016

JavaScript hoisting - Q&A

Question:

What’s the result of executing this code and why?










Answer

The reason is that both variables and functions are hoisted (moved at the top of the function) but variables don’t retain any assigned value. So, at the time the variable a is printed, it exists in the function (it’s declared) but it’s still undefined. Stated in other words, the code above is equivalent to the following:



No comments:

Post a Comment