Should "const" be favored over "let"?

# Glen Huang (5 years ago)

I've completely replaced "var" with "let" in my es 2015 code, but I noticed most variables I introduced never change.

Should I replace them with "const"? Will there be any performance gain for most browsers? If so, should such performance gain be considered micro optimization?

# Frankie Bagnardi (5 years ago)

I've switched to let/const completely. The gain of using const isn't performance (you can statically analyze whether any non-global is potentially assigned to). The gain from const is that it's very very easy for a human to statically analyze.

If I see a let binding, I know I need to be a bit more careful with editing that piece of code.

I originally thought this'd be a terrible idea, but it's been working great for me.

# Alex Kocharin (5 years ago)

An HTML attachment was scrubbed... URL: esdiscuss/attachments/20150417/153257c4/attachment

# Mathias Bynens (5 years ago)

On Fri, Apr 17, 2015 at 7:53 AM, Glen Huang <curvedmark at gmail.com> wrote:

I've completely replaced "var" with "let" in my es 2015 code, but I noticed most variables I introduced never change.

Note that const has nothing to do with the value of a variable changing or not. It can still change:

const foo = {};
foo.bar = 42; // does not throw

const indicates the binding is constant, i.e. there will be no reassignments etc.

In my post-ES6 code, I use const by default, falling back to let if I explicitly need rebinding. var is for legacy code.

# Glen Huang (5 years ago)

@Mathias

Thanks for the clarification. I should use "never rebind".

@Alex @Frankie

You guys are right. There won't be any performance gain since it's already statically analyzable.

FWIW, I opened a feature request

# Andreas Rossberg (5 years ago)

On 17 April 2015 at 14:17, Mathias Bynens <mathiasb at opera.com> wrote:

On Fri, Apr 17, 2015 at 7:53 AM, Glen Huang <curvedmark at gmail.com> wrote:

I've completely replaced "var" with "let" in my es 2015 code, but I noticed most variables I introduced never change.

Note that const has nothing to do with the value of a variable changing or not. It can still change:

const foo = {};
foo.bar = 42; // does not throw

const indicates the binding is constant, i.e. there will be no reassignments etc.

I have to nitpick on this. In usual nomenclature of programming language semantics the "value" in question is the reference to the object (for types with reference semantics), and that does not change. That the object itself actually can be mutable is a separate property.

The idea that const somehow applies transitively is a specific idiosyncrasy of low-level languages like C and friends, that make flattening and copying of mutable objects an implicit part of their semantics. It is not the norm, and actually rather contorted semantically.

# Allen Wirfs-Brock (5 years ago)

On Apr 17, 2015, at 5:09 AM, Alex Kocharin wrote:

There won't be any performance gain. "const" is used to be much slower in v8 actually. But they fixed it as far as I know.

I think it's a code style matter. And speaking about that, realistically, most code base will never use "const" widely. Just one reason: 5 characters vs 3 characters to type. So in the name of keeping an amount of different code styles smaller, I'd say stick with "let" (except for obvious constant literals like const PI = 3.14 on top). Just something to consider.

I agree, 'let' is likely to win because of it's length. I find that I fall into using it solely or that reason. I think it also wins on readability.

If we had a "do-over". I'd make let means what const now means and have something different for defining mutable lexical bindings. Maybe let var foo=...;.

But the let/const pairing was a firmly established direction long before work on ES6 even started. There was so much other stuff to work on and so much inertia behind let/const that nobody ever seriously challenged that direction.

# Marius Gundersen (5 years ago)

I too have found that most of my variables are defined with const, as they never change. Let var die is a catchy saying, but I believe it should be let var die, use const.

On Fri, Apr 17, 2015 at 6:16 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>

wrote:

I agree, 'let' is likely to win because of it's length. I find that I fall into using it solely or that reason. I think it also wins on readability.

If we had a "do-over". I'd make let means what const now means and have something different for defining mutable lexical bindings. Maybe let var foo=...;.

let and mut? Oh well, too late for that now.

Marius Gundersen