Declarations in Conditionals S1
- Stage: Stage 1
- Status: Active
- ECMAScript edition: —
- Synchronized: Sep 19, 2026
- 中文译文 · Source repository
The proposal lets developers declare variables inside the condition of if and while statements using let, const, using, and await using. A declaration must end with an explicit ; followed by a second expression that is actually tested, which avoids ambiguity with existing destructuring syntax. This pattern evaluates initializers only once and keeps bindings scoped to the relevant block.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Declarations in Conditionals
ECMAScript proposal for allowing variable declarations inside conditional statements (e.g. if/while).
Authors:
- Devin Rousso
Stage: 1
Candidate spec text is available.
Overview
When programming in C++, an extremely useful feature is to be able to declare a variable inside a conditional before evaluating its condition:
Adding this capability to JavaScript would be very useful for the following reasons:
- avoid having to evaluate the initializer more than once
- finer-grain "control" over the visibility of the variable
- allow authors to write performance-"safe" code without having to know the specific details of the code being called (see example below)
In the case of JavaScript, however, there should be some limitations:
- only using
let,const,using, andawait using - only for
ifandwhile - only exposed in the
ifblock (i.e. not in theelse) - declarations always require an explicitly written
;followed by a second expression that specifies what is tested
Limitations
In sloppy mode, the following is unfortunately already valid JavaScript
where let is parsed as an identifier, so let[x] = y is a computed property assignment rather than a destructuring declaration.
If if (let x = y) were allowed to omit the second expression, developers might reasonably expect the form above to be its destructuring counterpart despite its existing different meaning.
That expectation would be especially natural because, once an explicitly written ; and a second expression are present, both if (let x = y; x) and if (let [x] = y; x) declare x and then test the following expression.
Requiring this form for every declaration avoids the mismatch.
Examples
Here's an example of where allowing declarations in conditionals could be useful:
could be replaced by
which evaluates foo.data only once while keeping data scoped to the first branch.
One could create another variable (e.g. let data = foo.data;), but that could potentially keep foo.data (via data) alive much longer than needed and would "pollute" the scope with an additional variable.
As another example, a non-module <script> needs an extra block to limit the scope of a temporary binding:
which becomes unnecessary if we can move the declaration to the condition:
Transpiler Support
This can be transpiled using blocks and a generated label:
Note that __if0 represents a fresh label chosen by the transpiler so it cannot conflict with any label in the source.