Great first post! Way too much confusion about the differences between var/let/const going around.
Just wanted to point out a small mistake in your post. var declarations aren’t hoisted to the global scope neccessarily. They’re hoisted to the top of their scope which is function-based.
So in your example the variable name would have been hoisted to the top of the function (above the if-statement) and not out of the function.
I use the same approach regarding defaulting to const and using let when the value needs to be changed. Although i’m quite sympathetic to Kyle Simpsons approach of using var by default and using let whenever you specifically want a block-scoped variable and const when you want to express that the value should not be changed.
This makes it more clear to other developers (including future-us) what the intention of the code is.
Hi Dennis,
Great first post! Way too much confusion about the differences between var/let/const going around.
Just wanted to point out a small mistake in your post. var declarations aren’t hoisted to the global scope neccessarily. They’re hoisted to the top of their scope which is function-based.
So in your example the variable name would have been hoisted to the top of the function (above the if-statement) and not out of the function.
I use the same approach regarding defaulting to const and using let when the value needs to be changed. Although i’m quite sympathetic to Kyle Simpsons approach of using var by default and using let whenever you specifically want a block-scoped variable and const when you want to express that the value should not be changed.
This makes it more clear to other developers (including future-us) what the intention of the code is.
Thanks for the great read!
Thanks! Fixed it in the post :)