Const seems useless vs let


#1

I’m sure I just don’t understand

Const means something will stay the same, so in the future If I wanted to change the value I couldn’t.
but if I use let then it can change, but it doesn’t HAVE to change, so it seems like let does everything const does and more.

So maybe someone could tell me why const is any good and show an example where it would be better to use const instead of let?

thanks


#2

so far i know there are two advantages:

With const you’re explicitly telling the engine that the value cannot change¹. So it’s free to do any optimization it wants, including emitting a literal instead of a variable reference to code using it, knowing that the values cannot be changed.

And you can’t accidentally overwrite a variable which shouldn’t change. Which is useful if your code base is growing.


#3

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