Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

41–50 of 69 posts

Re: JavaScript variables: var and let and const

#42
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"

> 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?"

If the value is a primitive, you can't change it. If the value's a reference you also can't change that, but of course you can modify whatever that points to.

Is that what's bugging people about this?

Re: JavaScript variables: var and let and const

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

If you like terse code I recommend APL.

Re: JavaScript variables: var and let and const

#46
post #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) { ... } })()

That may be a nice addition to the "do expression" proposal for javascript: https://github.com/tc39/proposal-do-expressions

    let x = do {
        let tmp = f();
        tmp * tmp + 1
    };
Unfortunately it seems like the proposal doesn't get much attention.

Re: JavaScript variables: var and let and const

#47

My inner pedant really hates this first sentence: > There are three ways to create variables in a JavaScript application: using var, using let, or using const. No. There are two ways to create variables (let, var), and _one_ way to create a constant (const).

Your inner pedant isn't being pedantic enough, then, because the ES spec defines them all as variables.

> 13.3.1 Let and Const Declarations

>

> NOTE let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment.

http://www.ecma-international.org/ecma-262/6.0/#sec-declarat...

Re: JavaScript variables: var and let and const

#48
post #39
post #26

Earlier quoted context omitted.

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`)

Interesting, but not really tractable if you have to manually add every single variable to the rules config.

Re: JavaScript variables: var and let and const

#49

Earlier quoted context omitted.

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"

> 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?" If the value is a primitive, you can't change it. If the value's a reference you also can't change that, but of course you can modify whatever that points to. Is that what's bugging people about this?

I believe so. I don't make a big issue out of it... but anything carrying the word const I expect it to be fully read-only with no "buts" in between throughout the whole execution of the program. I guess if they used "readonly" as C# it would make more sense?

Re: JavaScript variables: var and let and const

#50
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.

Swift’s var and let are also good. There is no “default” but “let” is obviously preferred
Post reply on HN