Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

51–60 of 69 posts

Re: JavaScript variables: var and let and const

#51
post #48
post #39

Earlier quoted context omitted.

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.

The plugin allows pattern-based exceptions.

For example, if your rule excluded variables with a "mutable" prefix, you could name your variable mutableSomething and it will be automatically ignored by the linter

Re: JavaScript variables: var and let and const

#52
post #18

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"

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.

Thanks, I've never checked out Babel. I will truly suggest that to my lead, seems something we can take advantage of.

Re: JavaScript variables: var and let and const

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

    try {
        const { attribute1, attribute2 } = getObject()
    } catch () {}
    attribute1 // undefined
How would you solve that?

Re: JavaScript variables: var and let and const

#54
post #35

Use const always, unless you must use let. That's it. You don't need to read other articles on this subject, it's that simple.

`var` creates global properties when used in the global scope, which is useful at times.

If you really, really need a global then you should assign it to `window/global/globalThis` instead of relying on the weird behavior of `var`

Re: JavaScript variables: var and let and const

#55
post #23

There's one non-obvious corollary from the fact that using let/const before declaring throws an error and var does not: If you have let/const in your source code, but you use babel to transform it to ES5, you may have cases where you use let/const in that error-throwing way, but it's compiled down to var, and all is good (apart from variable being undefined when used). When you however upgrade your babel conf to outp…

So transpiling down to ES5 doesn’t preserve semantics ?

Re: JavaScript variables: var and let and const

#56
post #53

Earlier quoted context omitted.

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.

try { const { attribute1, attribute2 } = getObject() } catch () {} attribute1 // undefined How would you solve that?

With let outside the try, because that's the intended scope. Insofar as there is a problem I see with that, it's that you can't separate declaration of a single-assignment value (const) from the one allowed assignment, because the ideal solution would be const declared outside the try with the assignment inside.

OTOH var declared inside is a bad solution because it's both misleading as to scope and using a freely-assignable variable for something that's logically single-assignment. Let outside has only one of those problems.

Re: JavaScript variables: var and let and const

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

Mind that JavaScript simply reused the set of reserved keywords from Java. This similarity in keywords and syntax is, what made the "Java" part in "JavaScript", while there is actually no deeper relation between the two languages. `const` was available. (However, `let` wasn't in that set, nor in the reserved ECMA extension.)

Re: JavaScript variables: var and let and const

#58
I use `let` mostly and I only use `const` for special cases when changing the variable would break the program and I want to constrain future developers from doing that. For example, when I import some library references in Node.js using `require`, I will use `const` or if I need to define a magic number like `const HANDSHAKE_TIMEOUT = ...`.

I don't see the point of using const everywhere though because I don't code in a pure functional style and I don't pretend to.

Often, it makes sense to re-assign a variable and I don't want to mislead future developers into thinking that it's not OK to do so if the requirements have changed.

Re: JavaScript variables: var and let and const

#59

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

They're not really constant though, if you wanna be really pedantic. You can modify a const object for example.

Re: JavaScript variables: var and let and const

#60
post #59

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

They're not really constant though, if you wanna be really pedantic. You can modify a const object for example.

It’s constant because it cannot be reassigned - but it is not immutable.
Post reply on HN