r/javascript • • Oct 24 '16

help The only thing I hate about const ...

... is that it is 5 chars long.

For some reason, let with its 3 chars, looks more elegant.

7 Upvotes

49 comments sorted by

View all comments

4

u/saadq_ Oct 24 '16 edited Oct 24 '16

Man, this sub is volatile, haha. I got a pretty negative reaction when I mentioned the same thing a few months ago.

I agree with you, especially in cases where you have a mix of const/let variable declarations like:

const var1 = []
const var2 = {}
let var3 = 30
let var4 = 40

It would look neater if you used let everywhere (although of course you lose the added safety of const)

let var1 = []
let var2 = {}
let var3 = 30
let var4 = 40

Although I have seen a lot of people who do use let everywhere, and only use const for imports and for the old way of declaring "constants":

const fs = require('fs')
const MINIMUM_PAYMENT = 5

// Use `let` for everything else

I know Mozilla does it that way ^

4

u/Retsam19 Oct 24 '16

In fairness, "const is 5 letters long" is pretty off-topic in a thread about the performance implications of const; I'm not surprised you got flak for that reply.

1

u/saadq_ Oct 25 '16

Yeah that's true, but based on the responses it seemed like it had more to do with complaining about const rather than it being off-topic.

1

u/factorysettings Oct 24 '16 edited Oct 25 '16
const var1 = [],
      var2 = {};
let var3 = 30,
    var4 = 40;

?

1

u/saadq_ Oct 25 '16

I guess the argument I was making is that it looks nicer if you use exclusively one out of const/let because everything can line up:

let var1 = [],
    var2 = {},
    var3 = 30,
    var4 = 40;

1

u/inu-no-policemen Oct 24 '16

I know Mozilla does it that way

I also do it that way. If I have a handful of short-lived variables, I don't really care if they get assigned some value just once or more than once. The write and read aren't that far apart and you have to read both lines either way. There doesn't seem to be any tangible benefit. It doesn't simplify my mental model.

I'd only go with immutable bindings everywhere by default if they were less work. E.g. like "let" vs "let mut" in Rust.

What actually would simplify things is if the editor would color single-write variables (no matter if it's const or a single-write let) differently.