Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

61–69 of 69 posts

Re: JavaScript variables: var and let and const

#62

Earlier quoted context omitted.

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

What do you find weird about var?

Re: JavaScript variables: var and let and const

#63
post #53

Earlier quoted context omitted.

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-ass…

Exactly, I'm just pointing out the limitations of `let/const`

I usually solve that case with

    let attribute1, attribute2;
    try {
         const object = getObject()
         attribute1 = object.attribute1
         attribute2 = object.attribute2
    } catch () {}
But I'm just not satisfied with it, too many statements. And it gets really messy with many attributes

Re: JavaScript variables: var and let and const

#64
post #37

Earlier quoted context omitted.

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.

It would be nice if every JS statement had an expression equivalent.

Re: JavaScript variables: var and let and const

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

For some features, having 100% spec compliant behavior in transpiled code would mean excruciatingly slow/bloated code, or be downright impossible. There's a reason why features are added to the language after all: to add things that are impossible or too complex to write in old version of language.

Transpilation is "best effort" and involves some tradeoffs.

Re: JavaScript variables: var and let and const

#66
post #63

Earlier quoted context omitted.

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-ass…

Exactly, I'm just pointing out the limitations of `let/const` I usually solve that case with let attribute1, attribute2; try { const object = getObject() attribute1 = object.attribute1 attribute2 = object.attribute2 } catch () {} But I'm just not satisfied with it, too many statements. And it gets really messy with many attributes

How about this:

    let attribute1, attribute2;
    try  {
     ({attribute1, attribute2} = getObject())
    } catch (e) {}

Re: JavaScript variables: var and let and const

#67
post #63

Earlier quoted context omitted.

Exactly, I'm just pointing out the limitations of `let/const` I usually solve that case with let attribute1, attribute2; try { const object = getObject() attribute1 = object.attribute1 attribute2 = object.attribute2 } catch () {} But I'm just not satisfied with it, too many statements. And it gets really messy with many attributes

How about this: let attribute1, attribute2; try { ({attribute1, attribute2} = getObject()) } catch (e) {}

wow, didn't know that!

So parenthesis are the trick, thanks!

Re: JavaScript variables: var and let and const

#68
post #67

Earlier quoted context omitted.

How about this: let attribute1, attribute2; try { ({attribute1, attribute2} = getObject()) } catch (e) {}

wow, didn't know that! So parenthesis are the trick, thanks!

No problem!

Re: JavaScript variables: var and let and const

#69
post #62

Earlier quoted context omitted.

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`

What do you find weird about var?

It's weird because it has side-effects (namely, implicitly adding/setting a named property in the current scope object, if any).

It means that a top-level script using `var` that is subsequently refactored into a scoped function somewhere will now have different semantics and might break if other code depended on `var` creating a new global property.

Post reply on HN