Response to «const, let, and var. Why your linter is wrong.»
Here's a quick response to Caleb's post on using JS's var const, let, and var. Why your linter is wrong. 1:
I definitely agree: devs should recognize and reach for the tools we have available even when they don't reflect the current zeitgeist.
However, in your specific example of needing to declare a variable before assigning to it imperatively within an if or else block, another option we have at our disposal is wrapping that if...else in an immediately invoked function expression (IIFE) to force it to become an expression:
let foo = (() => {
if(condition) {
return 'bar'
} else {
return 'bbq'
}
})()
doThingsWith(foo)
Relatedly, I would love to see this TC-39 proposal make it into the spec: Proposal for do expressions. This would let you provide a block of imperative code with an automatic return of the last expression and without the extra syntax of the IIFE:
let foo = do {
if (condition) {
'bar'
} else {
'bbq'
}
}
doThingsWith(foo)
Note the
rel="snub:reply"in the anchor indicating that this article is a reply to the linked article. See my article on SNUB for conceptual details.↩