Turn JavaScript ternary expressions into if/else blocks

Paste statements that use the ternary operator and each one comes back as an if/else block, with nested ternaries unrolled into else if branches so the logic is easy to read and debug.

Live demo · real result from this tool

if/else version

let label;if (count === 1) {  label = 'item';} else {  label = 'items';}if (score >= 90) {  return 'A';} else if (score >= 75) {  return 'B';} else {  return 'C';}if (user?.active) {  status = 'on';} else {  status = (user ?? fallback).status;}
Statements converted3

Processed on your device — nothing is uploaded.

How to use the ternary to if/else converter

  1. Paste your code into the JavaScript box, one statement per line.
  2. Copy the if/else version. Lines without a ternary are kept as they were.
  3. Check Statements converted to see how many lines were expanded.

What it does and when to use it

A short ternary is neat. A nested one, such as a grade calculation with three outcomes, is hard to read, hard to step through in a debugger, and hard to extend with an extra line of logging. Use this tool when you review or refactor that kind of code, when you add a breakpoint to one branch, or when you teach how ? : maps to if.

The reverse direction and other refactors have their own tools: var to let / const modernises declarations, and the JavaScript formatter lays out the result. To confirm the converted code still parses, use the JavaScript validator.

How it works

Each line is checked against four statement shapes:

Shape Example Becomes
Declaration const x = c ? a : b; let x; plus x = a / x = b
Assignment total += c ? a : b; the same += in each branch
Return return c ? a : b; return a; / return b;
Expression c ? f() : g(); f(); / g();

The tool finds the first ? outside any brackets and the : that matches it, skipping ?. and ??. If the “else” part is itself a ternary, it becomes an else if branch, up to 50 levels. Brackets that wrap a whole condition are removed, and the original indentation is kept.

Worked examples

A declaration. const label = count === 1 ? 'item' : 'items'; becomes:

let label;
if (count === 1) {
  label = 'item';
} else {
  label = 'items';
}

A nested return. return score >= 90 ? 'A' : score >= 75 ? 'B' : 'C'; becomes an if, an else if (score >= 75) and a final else returning 'C'.

A compound assignment. total += isMember ? price * 0.9 : price; becomes:

if (isMember) {
  total += price * 0.9;
} else {
  total += price;
}

Not recognised. A ternary split over three lines returns “No ternary statements were recognised. Put each statement on its own line, e.g. const x = cond ? a : b;”.

Limits and tips

  • One statement at a time. A statement over several lines is skipped. Several statements on one line are split at their semicolons and converted one by one.
  • Side effects run the same way. Only the chosen branch runs in both forms, so function calls in the branches behave as before.
  • Destructuring declarations such as const { a } = c ? x : y; are left unchanged; rewrite those by hand.
  • Run the validator afterwards, since the converter matches text patterns rather than parsing the code.

Frequently asked questions

Why does const become let?
The variable is now declared first and assigned in each branch, and a const cannot be declared without a value. The tool writes let instead; var declarations stay var.
Will optional chaining or ?? confuse it?
No. The code is tokenized, so ?. and ?? are read as their own operators, not as the start of a ternary. In user?.active ? 'on' : 'off' only the lone ? is split.
My ternary is spread over several lines. What can I do?
Join it onto one line first, or run it through the JavaScript formatter with a wide print width. The tool works one line at a time and reports "No ternary statements were recognised" for multi-line ones.
Can it convert a ternary inside a function call or JSX?
No. A ternary inside brackets, such as foo(a ? b : c), is not a statement on its own, so there is nowhere to put an if block. Those lines are left unchanged.
Is the if/else version slower?
No meaningful difference. Choose whichever is easier for your team to read; long or nested ternaries are usually clearer as if/else.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close