Earlier quoted context omitted.
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.
JavaScript variables: var and let and const
61–69 of 69 posts
Re: JavaScript variables: var and let and const
#62Earlier 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`
Re: JavaScript variables: var and let and const
#63Earlier 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…
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 attributesRe: JavaScript variables: var and let and const
#64Earlier 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.
Re: JavaScript variables: var and let and const
#65There'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 ?
Transpilation is "best effort" and involves some tradeoffs.
Re: JavaScript variables: var and let and const
#66Earlier 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
let attribute1, attribute2;
try {
({attribute1, attribute2} = getObject())
} catch (e) {}Re: JavaScript variables: var and let and const
#67Earlier 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) {}
So parenthesis are the trick, thanks!
Re: JavaScript variables: var and let and const
#68Re: JavaScript variables: var and let and const
#69Earlier 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 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.