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 ^

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.