JavaScript declarations in conditionals was discussed at TC39 recently, but consensus broke down…
@FirefoxWebDevs90K followers
34,160 views
35 Comments
Comments from the original post on X (Twitter)

@kettanaito
Just posted about it last month! Yes, please, I would love this addition to the language. My answers to your questions below. 1. Syntax. if (let x = exp) {} if (const x = exp) {} 2. Conditional check. Probably the simplest approach would be the best. Don't overthink and don't assume for the developer. x != null truthfulness check is more than enough. I can argue that using iterators is a niche example here and if you do use it, be so kind as to handle any subsequent checks within the if closure. The important bit is to keep the syntax simple and predictable since it's going to stay around, well, forever. 3. Is the result available in `else`? I'm leaning toward no. If you adopt #2, there's no result, to begin with. The variable has no value, thus the else closure exists solely to react to that situation and there's no value for it to accept. Again, keeping it simple. If you have anything more complex, then this syntax is likely not your go-to choice. You bring up a great example of how additional language features, like disposable objects, make it confusing if the value is kept for the else block (again, assuming my truthfulness suggestion is disregarded).
10 likes

@soft_fox_lad
Is the shirt for sale anywhere? :0
9 likes

@auxiliarymoose
if (const value = foo(); value == 'one') { // stuff } else if (value == 'two') { // other stuff } if (using value = foo(); condition) { value.stuff(); } else { value.other(); } I think the above should be allowed. It would be surprising for value to go out of scope.
6 likes

@FirefoxWebDevs
This makes sense, but what about disposables?
1 like

@auxiliarymoose
Should be disposed at the end of else. if-else is logically a single block. Either the if branch or the else branch is entered, but it's mutually exclusive. They should behave symmetrically, with the only difference being that the if branch is entered when condition is true.
1 like

@imjcmartin
I just want blocks to return values so I don't have to use IIFEs
6 likes

@FirefoxWebDevs
Here's the proposal you're looking for, which is unfortunately stalled
2 likes

@imjcmartin
I wonder why not go with `const myVar: string | undefined = if(cond){ return "hi" }`. (Ignoring the fact that this is a simple case where a ternary would suffice). Do expressions look a bit... Hand wavey

@geoffdoesstuff
The most important thing is absolute clarity and documented behaviour. However I would be inclined to follow where Rust ended up, rather than where it started, as I suspect there are lessons in there...
5 likes

@ProEvilz
This feels like you're solving a problem that doesn't exist. It feels like this is being proposed 'just because', or because people are bored. JS doesn't need this whatsoever.
3 likes

@styfle
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?
3 likes

@auxiliarymoose
if (const { value, error } = foo(); error) { handleFailure(error); } else { handleSuccess(value); } Error-as-value approaches benefit from the variables being available inside else. Otherwise, you must rename each error and value in the outer scope.
3 likes

@medikoo
I'm following you. It should be available only in `if` clause
2 likes

@everdimension
Why invent a new semicolon syntax when it seems that && would suffice? if (const result = && !result.done()) {} This is much more intuitive
2 likes

@jaskptr
Having it only available in the if block is definitely safer. Consider this: if (f()) { return 1 } else {…} if (f()) { return 1 }; … They are equivalent. But in case #2, these wouldn’t be: if (const _ = f()) { return 1 } else {…} if (const _ = f()) { return 1 }; …
2 likes

@WebReflection
Fully agreed with Jake on scoping `if` only … if more than that is needed a let right before the `if` is all one needs to do, like it’s been since about ever, no big drama there, surely an easy way to reason about the feature … try/catch also works like that, no surprise there.
1 like

@FireNeslo
I'm not sure it's a very strong argument but I feel like if javascript had for(...) {} else {} you wouldn't expect to be able to access the index of the loop. So I don't really expect access in the else of an if either.
1 like

@FirefoxWebDevs
fwiw: for (…) if (…) else … …is valid syntax.
4 likes

@_dkannan
Why is Javascript even adding this? The proposal says that C++ has it (or Rust), which by itself is a red-flag. 😛 Jokes aside, the proposal also talks about some problems in transpiling maybe, which is a bigger (and actual) red-flag. 🚩 That too for something as fundamental as *if*. Why is this feature needed? And who is really asking for this feature? How will it behave with try-catch, promises etc? As an end-user, Javascript changing fundaments like *if* makes me uneasy. 😅
1 like

@ngriffin_uk
From my immediate glance at the code I don’t think else should have access to result when inside of a conditional, it just doesn’t feel right to me.
1 like












