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’m struggling with hoisting in Javascript!
I understand how hoisting work in JS.
I know that all declarations are hoisted in Javascript. (Not just var)
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.
Kind regards,
Moh
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.
How would the Javascript engine know what value to guess that you want if you don’t initialize the variable? There are an infinite number of possible guesses, especially since variable declarations are not typed in Javascript.
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?
If I’m understanding it correctly, did the JavaScript creators did so to make it like other languages, where you can’t use a variable before initializing it?
Letting 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 let and const were added to the language to give users tools to avoid these problems.
All variables are hoisted according to MDN.
Quote:
Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value. An exception will be thrown if a variable declared with let or 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?
The actual difference is that let /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.”