Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

31–40 of 69 posts

Re: JavaScript variables: var and let and const

#31
post #2

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

Those links are beautiful. It's nice knowing someone else out there has some sense. It makes me feel less alone.

But sadly the pedants won't let this go. The misuse of const is already documented as The Right Way, baked into libraries and live in production in millions of places. Sad but true.

I'd love to do some GitHub sleuthing and find the first few developers out there to publish a project that misused const in their code, so we could tar and feather them. Though obviously being some sort of fad or mass delusion, it wasn't entirely their fault.

Re: JavaScript variables: var and let and const

#32
post #2

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

The OP article is common knowledge and is a copy of a copy of a copy. Your first link, on the other hand, is the perfect recipe for my Friday afternoon. Cheers!

it's also wrong in the first sentence. there are at least 5 ways to define variables in js (edit: hint: globals)

Re: JavaScript variables: var and let and const

#33
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…

Genuine question (and hoping for your opinion here), why would I use Kotlin over Java? Outside the obvious like developing on Android.

I haven't had much exposure to it, but it seems like it has a wider breadth of features and has a quicker development speed?

Re: JavaScript variables: var and let and const

#34
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…

Genuine question (and hoping for your opinion here), why would I use Kotlin over Java? Outside the obvious like developing on Android. I haven't had much exposure to it, but it seems like it has a wider breadth of features and has a quicker development speed?

http://steve-yegge.blogspot.com/2017/05/why-kotlin-is-better...

Re: JavaScript variables: var and let and const

#36
post #29

the one thing that annoys me about let/const that leads to me occasionally using var is that with var you can declare a variable in a try block and then use it afterwards, instead I have to declare a let outside the try or put way more then I want to in the try.

But, why would you want the declaration in the try when the scope of the variable is broader? It's true that var let's you do that, but it seems to me that's strictly a misleading thing.

Sure, you’ll often need to split declaration from assignment with let in that case, but that's not a bad thing, IME.

Re: JavaScript variables: var and let and const

#37
post #29

the one thing that annoys me about let/const that leads to me occasionally using var is that with var you can declare a variable in a try block and then use it afterwards, instead I have to declare a let outside the try or put way more then I want to in the try.

It would be nice if you could assign the result of a try / catch (or any block) to a variable, you can kind of emulate it with an IIFE:

    const outer = (() => {
      try {
        ...
        return ...
      } catch (error) {
        ...
      }
    })()

Re: JavaScript variables: var and let and const

#38
post #2

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

I read a whole bunch of that tweet thread and got nothing out of it. What's wrong with const? Been using it for quite a while now, seems fine, no trouble. What am I missing?

Re: JavaScript variables: var and let and const

#39
post #26

Earlier quoted context omitted.

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, b…

And in TypeScript you can at least pair it with Readonly , though that unfortunately still isn't recursive

Nice plugin to make sure you never mutate: https://github.com/jonaskello/tslint-immutable/tree/master/t...

You can add a rule based on the name of the variable to allow mutating (e.g. naming it `mutableSomething`)

Re: JavaScript variables: var and let and const

#40
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?

JavaScript not only has (a small set of) value types, it also has frozen objects.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Post reply on HN