Post

JavaScript declarations in conditionals was discussed at TC39 recently, but consensus broke down over one key detail. How do you think it should behave?

22 reposts110 likes

I don't think it should be exposed in the `else` block. That setup creates opportunities to misuse the `null` value, which is what we're trying to avoid! 😅 I think JS should stabilize features that accentuate correctness instead of comfort -- there are already too many ways to mess this up lol

Seems like the variable should be scoped to the if only, not the else. Devs that want to use the variable in the else statement can hoist the variable up, right?

2. Validity in the else block should not be done, because a value should always be scoped to a single block. If you want validity in both blocks simply do `{const result=…;if(result?.valid){}else{}}`

I think most developers would be _surprised_ to know that it's scoped to the else too. Yes, thinking about it a bit more, they would agree that it _has its uses_, but they'd still be surprised and lean toward it not being scoped to the else. The majority of the answers here be like that.

Also, there is _no way_ to have a variable that is scoped to only the `if`, whereas there is a way to have a variable that is scoped to both if and else. So I would go with scoping only to the if also because of this - it's the only way to do it!

I don't know that it shouldn't be available in the else block. In that specific example I can't imagine one would often be using `resource` in the else block but what if it went into a few `else if()` blocks first? I could imagine a need for `resource` to still be around in an `else if()` block.

Well my opinion on this is weak either way because regardless of the final convention, there are ways to write the code to make memory free when you want and they don't seem particularly burdensome. Eg. if Rust-like you could just declare the viable outside the if in a using block to keep it scoped.

That said, I think if had to guess the behavior without looking at documentation I would guess that it's still available in the `else` so that's slightly more natural seeming to me...but I wouldn't be frustrated to learn it wasn't and the rust way would bias towards less resource usage.

If we go with "visible in else" it's actually pretty hard to get disposal to happen before the else! Try rewriting this without relying on "dispose before else": if (using resource = getResource(); resource.needsUpdate) { resource.update(); } else { await postMessage('no updates required'); }

It shouldn’t be in the else block be cause “else if” exists (I know it’s just an inline else followed by a blocky if). No one would expect it to be available in the else if I don’t think. Therefore it shouldn’t be in the else.

I’d initially say it should be scoped to the else block too, because if the custom truthy-ness is false, the rest of the object might contain an error message which could be useful. But then I suppose for that use case, sticking to the current syntax makes more sense.

I’m not 100% sold on my opinion here. Without the custom check, I’d immediately say scope to `if` block only because you most likely wouldn’t do anything with a falsy value. So yeah, sticking to the new rust behaviour of else being out of scope is probably what i’d say is better

My original thought was this was scope-blocked to the if until you stated the question. This feels more consistent with scope blocking elsewhere, so my two cents is it should not be made available. If you need the var accessible elsewhere (pun not intended), the current syntax today is clear.

Great explanation! The other choice is: if it isn't available in the else, can you still refer to that identifier from a parent scope? My opinion: not available in the else, and referring to the identifier is a syntax error so there can be no ambiguity for the reader.

does JavaScript have syntax errors for misreferencing variables? you usually find out the hard way at runtime with a `ReferenceError`

You're right that there isn't AFAIK an early syntax error for a bad reference, only invalid declarations. This came up in yesterday's TC39 meeting, it will need more research to see if this could even be implemented efficiently

Oh I missed the last meeting so I didn't realize it had been discussed! Goddam I need to get back on the pattern matching proposal, it would be a real shame to have this advance with a totally different syntax.

Based on the current scoping of variables I would prefer to make it only in the if available. If it's needed in the else the variable should be defined outside the if like currently. The stated problem with the resource reinforces my opinion in this regard.

I really don’t like shoving multiple expressions into the if like that… there has to be a better way right?