Why are let and const are not initialized in Javascript?

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’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

because it avoids this behaviour

console.log(a)

var a = 9;

We can use a var variable before initialization without issues.

image

console.log(b);

let b = 4;

Here it let me know that b is not defined
image

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.

  1. const must be initialized, not doing so is a syntax error.

  2. 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)

https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-let-and-const-declarations

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.

2 Likes

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?

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 let and const.

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 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.”

Appendix A: What’s the Deal with TDZ?

It is a bit hard to find historical context but here is some I found.

https://mail.mozilla.org/pipermail/es-discuss/2011-August/016188.html


Here is another explanation of it from a TC39 member.

1 Like

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!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.