Are there any performance gains from using `const` instead of `let`?

If not then I don't understand why people do it.

Reply to this note

Please Login to reply.

Discussion

I don't think so 🤔 I think it's more to show an intend to the reader. I always use const unless I really need a let. But honestly that happens maybe twice a year, being a full time web dev 😃

And there's still var 😁

Well and then reassigning const throws an error, so at least you'll know when you try to change a variable you probably shouldn't change

const clearly states intent, it's good for longevity of the code especially with multiple maintainers. Not sure about performance, I can imagine a runtime can make some assumptions for performance if it knows it's a const.

I have absolutely no idea what you're asking. What I do know is that you're a "Zen Master" at this kind of shit🤷🏻‍♂️. Just meditate a bit, the answer will come🙏🏻.

Everyone knows typing 5 characters is faster than typing 3

My editor makes "const" variables yellow while "let" variables are white. I don't know what sane person prefers yellow variables.

common sense, really

Security is probably a bigger concern. Sometimes you want to make sure a value does not change after assignment. Like when you have a process dealing with personal / sensitive data, account numbers, #sats, etc.. :)

If you're concerned with security should you really be writing JavaScript? `const` doesn't really mean a constant value.

Bc the linter gets mad if I don't

This may be the correct explanation.

Zig had ‘let’ and ‘let mut’ at one point, then removed them in favor of just ‘const’ and ‘var’. https://github.com/ziglang/zig/issues/181

we could go back to using var

performance, probably not.. immutability yes.

const immutableObject = {}

Not performance, just mutability safety.

In terms of performance the difference is minimal, if we get detailed `const` helps the compiler to optimize performance by ensuring that the reference remains constant, on the other hand ‘let’ minimally overloads the compiler as it must allow it to overwrite.

We would optimize more by cleaning the fans of the pc than with this 🤣🤣🤓

Then please start writing `let`!

🫡🫡🫡🤓🤣

Maybe there is a preference towards constant unchangeable variables; that’s why.

Depends on the language