Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

11–20 of 69 posts

Re: JavaScript variables: var and let and const

#11

Earlier quoted context omitted.

Does JavaScript have value types where const might work the way the first author expects it to?

No. The only thing that makes const unique from let is that references declared as constants cannot be reassigned or else an error will be thrown. The exclusion only applies to the reference name itself and not to any of its properties as the article articulates.

[deleted]

Re: JavaScript variables: var and let and const

#12
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

Lol that first read is fantastic. const doesn't do shit and we all know it Was a great intro, and absolutely true. Unfortunately I don't think this will catch on, although I agree with the author that it should.

I like the reassignment prevention.

Re: JavaScript variables: var and let and const

#13
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

Does JavaScript have value types where const might work the way the first author expects it to?

symbol/boolean/string/number are immutable and work how you'd expect with const because they are immutable. Unsure if that makes them "value types".

I think the article is a bit silly. Calling it "const" was a mistake but that's a naming issue. JS's "const" is the same as Java's "final," right? It communicates something useful.

Re: JavaScript variables: var and let and const

#14
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?"

I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"

Re: JavaScript variables: var and let and const

#15
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

I think one important point that Dan and others miss is that `const` enforces a single-assignment style of variable naming that many find preferable. It makes it easier to read and forces people to provide more thoughtful names for their variables. I'm not saying that this behavior is enough to justify using `const` everywhere, but it should be weighed in any judgement about style.

Re: JavaScript variables: var and let and const

#16
post #8
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

I like that Swift has var and let. Having the first letter be different means less typing with autocompletion. I suppose when I get to use voice recognition for coding, there will also be less confusion.

Generally speaking most languages treat `let` as constant/immutable, and the real shame is that JS did not. It's understandable why (they needed a block-scoped `var`), but unfortunate nonetheless.

Re: JavaScript variables: var and let and const

#17
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

IMO the article's conclusion is wrong. We should indeed use `const`, and we should indeed use it to communicate. But the thing we should use it to communicate is different than what the article proposes.

We should use `const` to communicate that we don't intend to reassign or mutate the variable. And we should do that by default unless there's a compelling reason to do otherwise.

I know it can't be fully enforced, because JavaScript is designed for mutability of objects by default. But it is an implementation style that is almost always achievable in plain ES6 without too much trouble, and it makes more reliable software that's easier to reason about.

Re: JavaScript variables: var and let and const

#18
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?" I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"

If you're willing to add a build step to the project, Babel will let you use all the new features and compile down to whatever version you need.

Re: JavaScript variables: var and let and const

#19
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

Does JavaScript have value types where const might work the way the first author expects it to?

I won't repeat the other comments, but I'll note that you can make the additional, uglier step to get the results the first author expects by (as suggested) treating the assignment as a pointer to the object and rendering the object immutable.

    const myObject = Object.freeze({
        myValue: "Something",
    });
The problem with that is `Object.freeze` doesn't act on nested objects, so you usually have to have some other method/macro to do that for you. You might be able to do that with decorators in TS.

It works on Object and Array types, but not on other primitive types. That part is frustrating. It would certainly be nice to have a single method for rendering any value immutable, whether it was the initial assignment or a built-in.

Re: JavaScript variables: var and let and const

#20
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

A solution I prefer is Rust's where the immutable binding is default "let x" and you opt-in with a modifier to make it mutable "let mut x".

It just makes sense for the immutable case to be default by design instead of just by developer habit.

"var" vs "val" always were too similar for me.

Post reply on HN