Var VS let keywords

In the first ES6 challenge it explains that let was added because var can accidentlly be overwritten without causing any errors.

My question: why didn’t they reverse it in ES6?

To me, it seems like “let” is less permanent language than “var”… like “ohhh I will let i = 5 for now, but later I might let i = 6”

Basically, what I’m getting as is I think it would make more sense if let was the one that could be overwritten and not var.

Would this just have been too big of a change for ES6?

What challenge was it?
The one that can’t be overwritten is const (const x = 8 if you later reassign x throw an error or just ignore the reassignment)
With var is good practice to not redeclare a variable, let will just not make you do it. But with both var and let you can change what the value of the variable is as often as you want.

let and var are more similar than you think, but still different
It would have been a breaking change to have a different behaviour for var when redeclared, so they couldn’t do it.

1 Like

It has to do with the fact that the var keyword is “hoisted”, so any uses of var, anywhere in a function body, are moved to the beginning of the function body. let, on the other hand, is defined “in place” – so it may not exist on line 5, but if you define it about line 493, then THERE it does.

Why does this matter? In order to do as you say, and allow var to “reverse” as you suggest, it would require redefining the whole hoisting conversation. And, from a legacy coding perspective, a LOT of sloppy code that relies on variable hoisting would then break.

let allows for a little more flexibility in WHEN variables can be created, but that means it has to be more strict in whether they overwrite existing variables of the same name.

With var, because every instance is brought to the top of a function body, if there are multiple definitions the last definition is the one that sticks.


It was this one:

Technically, let and const are hoisted, just that they are left uninitialized. But if you want to think of them and unhoisted, that is probably useful.

Another big difference is that var is functional level scope and let and const are block level scoped. This can have big consequences in code.

1 Like

I see now… indeed, you cannot declare a variable again with let, the difference is

var name = "Kim" The name variable has been declared with a value of Kim
var name = "Carol" The name variable has been declared again, this time with a value of Carol

let name = "Kim" The name variable has been declared with a value of Kim.
let name ="Carol" Error! name has already been declared!
name = "Carol" Okay, we’re good now…the name variable is given a new value of Carol.

The difference is…if you use var, you very well may at some point in your code declare another variable of the same name without realizing it which can cause issues. Use let, and you absolutely will not be allowed to make that mistake.

With const, just like let, you cannot declare a variable more than once… however, unlike with let, you cannot assign const a new value.

1 Like

Can’t do things like that, it would break large chunks of the internet. Can’t deprecate or change existing functionality except in very specific, unusual cases. For example, getYear and setYear don’t work properly (Y2K issue), but new methods had to be made instead of fixing how they work, their functionality cannot be changed and they can’t be removed from the language.

Regards var/let, let has generally much better characteristics (what you describe isn’t generally useful behaviour) and it is essentially a better var, but how var works cannot be changed under any circumstances.

1 Like