| Field | Value |
|---|---|
| Proposal | if return <expression>; |
| Authors | @NookieGrey |
| Champion | Seeking champion(s) |
| Stage | 0 |
| Process | TC39 process document |
This repository contains an early Stage 0 draft. The main immediate goal is to find TC39 champion(s) to refine scope, syntax, and semantics for potential Stage 1 presentation.
In JavaScript code, a common early-return pattern repeats often:
const value = compute();
if (value) return value;The proposal introduces a compact statement form for this exact pattern.
Proposed syntax:
if return expression;Intended behavior:
- Evaluate
expressiononce. - If the result is truthy, return that result from the current function.
- Otherwise continue execution.
Equivalent desugaring model:
const _temp = expression;
if (_temp) return _temp;_temp above is explanatory only; no user-visible binding is created.
function getCachedOrNull(key) {
if return cache.get(key);
return null;
}function getGoodsPrice(id) {
if return getPrice(id);
if return getLastPrice(id);
return 0;
}function read() {
if return maybeExpensiveRead();
return "default";
}maybeExpensiveRead() is evaluated once.
- This proposal does not add an
elsebranch toif return. - This proposal does not change JavaScript truthiness rules.
- This proposal is not currently an expression-form construct.
- Is
if returnthe best keyword order, or should alternative spellings be considered? - Should this form be allowed everywhere
returnis allowed, or in a narrower context? - Are there parsing or readability concerns with ASI and statement boundaries?
- Should tooling enforce/forbid this shorthand in specific style profiles?
Userland helpers cannot short-circuit with function-local return. This pattern can only be made concise with syntax.
Looking for TC39 champion(s) to help with:
- Stage 1 viability assessment.
- Syntax and grammar risk review.
- Agenda strategy for initial TC39 discussion.
- Consensus-building on alternatives.
See /docs/champion-outreach.md for a concise outreach message.
Not required for Stage 0, but useful for feedback quality:
- A Babel prototype transform (optional).
- ESLint/parser experiment support (optional).
- Example corpus rewrite to measure readability impact.
This repository includes a minimal Babel prototype in /transpiler/babel-plugin-if-return.js.
Run it on the sample:
npm install
npm run transpile:exampleInput:
/examples/input-if-return.js
Output:
/examples/output-if-return.js
Current PoC limitation: custom syntax is recognized only for standalone single-line statements in the form if return <expression>;.
Early spec text lives in /spec.emu and is intentionally minimal at Stage 0.