Using the code
Do you know?
The below two scripts will return same output.
<script> x = 5; alert(x); var x; </script>
<script> var x; x = 5; alert(x); </script>
Limitations of Hoisting
Even though the process of Hosting will move the declarations to the top, there is some limitations too. We will discuss it here now.
The below scripts will give different outputs.
<script> var x = 2; var y = 4; alert(x+y); </script>
This will give you an output of 6.
<script> var x = 2; alert(x+y); var y = 4; </script>
So the second example is same as of below.
<script> var x = 2; var y; alert(x+y); y = 4; </script>
This will give you an output of NaN.
Since we are all developers, it is always makes things simple. So much complexity in software comes from trying to make one thing do two things. So always declare your variables on top of your function or scope. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.