Ternary to If/Else — Free Online Tool

Expand ternary assignments, declarations and returns into readable if/else blocks, safely skipping ?? and ?. operators. Ternary to If/Else is a free online tool from the ToolsRift JavaScript Tools collection. It runs entirely in your browser — nothing to install, no signup, and your data never leaves your device.

Loading Ternary to If/Else

How to use Ternary to If/Else

Paste statements that use the ? : ternary operator, one per line, then click Convert to expand each recognised statement into an if/else block.

Frequently asked questions

Which statements are converted?

Three forms per line: a declaration (const/let/var x = c ? a : b), a plain assignment (x = c ? a : b) and a return (return c ? a : b). Other lines are left unchanged.

Does it touch ?? or ?.?

No. The nullish coalescing operator ?? and optional chaining ?. are detected and skipped so they are never mistaken for a ternary.

How are nested ternaries handled?

The outer ternary is expanded and any nested ternary is kept intact inside the branch, so the code stays valid — run it again to expand deeper levels.

More JavaScript Tools

List to JS Array
Turn a list of lines, comma or space separated values into a JavaScript array literal with quoting, trimming and de-duplication
JS Variable Name Validator
Check whether a name is a valid JavaScript identifier and explain each problem — digits, symbols, spaces or reserved keywords
JS Quote Normalizer
Convert JavaScript string quotes between single and double, string, comment and regex aware, re-escaping quotes as needed
JSON Key Sorter
Recursively sort the keys of a JSON object alphabetically (ascending or descending) with configurable indentation
var to let / const
Replace the var keyword with let or const across your code, string, comment and regex aware so text like \
JavaScript Formatter
Beautify and format JavaScript code with proper indentation and structure for better readability

← View all JavaScript Tools