Hi, fellow campers! Hope you’re doing great!
NOTE: I’d like to appologize about my English, I used Grammarly to fix some issues, but not sure if everything is correct here.
I understand how hoisting work in JS.
- I know that var variables are set to “undefined”, while let and const are not initialized with a default value.
So, my question is: why is it like this?
I mean, why let and const are not initialized? Is there some wise reason behind this feature/behavior? Or is it just a decision that was made by the JS creator?
Based on my research, I have a theory about why const is not initialized.
Basically, a const should have an identifier and a value in the same line, otherwise, it’d be incorrect:
const consName = value;
So it makes sense that the JS engine throws a syntaxError when trying to access a const variable (I know, it’s constant :)) because its syntax is wrong in the first place.
I don’t know if my theory here is correct, but it seems plausible.
I couldn’t find an explanation for why let is not initialized.
I would appreciate any explanation for this phenomenon, as a teacher, it’s really important for me to understand how things work under the hood.
because it avoids this behaviour
var a = 9;
We can use a
var variable before initialization without issues.
let b = 4;
Here it let me know that
b is not defined
I do not think that this is true. Well, it depends upon how you are meaning hoisting. Let and const are not hoisted in the way that var are.
const must be initialized, not doing so is a syntax error.
let is initialized to
undefined when evaluated (if no initial value was given).
If a LexicalBinding in a
let declaration does not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.
MDN: Temporal dead zone (TDZ)
So, “It avoids this”, means that initializing a variable to undefined is not a good thing, and that’s why they added let and const where they’re not initialized to undefined?
var hoist (or worse be undeclared!) and be used before it is even defined or has a value assigned generated a bunch of confusing bugs, so
const were added to the language to give users tools to avoid these problems.
All variables are hoisted according to MDN.
Variables declared with
const are also hoisted but, unlike
var , are not initialized with a default value. An exception will be thrown if a variable declared with
const is read before it is initialized. End Quote.
But I get your point. At the end, let and const are treated in a way that makes them seem not hoisted.
So again, it seems that let and const are not hoisted in the same way as var because JS creators wanted to make JS behave like other languages where you have to initialize variables before using them? I’m I right here?
I thought this works only with var…
The temporal dead zone is weird. Look at that link above.
This funky behavior is due to JavaScipt patching some surprising behavior of
var with the better behaved
Exactly! Until now, I understood that let and const where added to fix var flaws.
But, because they don’t behave like var when it comes to accessing them before declaring them, the TDZ term was introduced.
It’s strange to me because I have some experience with Java, and this whole issue of using a variable before declaring it is REALLY strange.
Uninitialized Variables (aka, TDZ)
The actual difference is that
const declarations do not automatically initialize at the beginning of the scope, the way
var does. The debate then is if the auto-initialization is part of hoisting, or not? I think auto-registration of a variable at the top of the scope (i.e., what I call “hoisting”) and auto-initialization at the top of the scope (to
undefined ) are distinct operations and shouldn’t be lumped together under the single term “hoisting.”
Appendix A: What’s the Deal with TDZ?
It is a bit hard to find historical context but here is some I found.
Here is another explanation of it from a TC39 member.
You are a true great contributor! Thank you so much for this answer.
And thank you all guys! I’ve learned many things from each one of you!