Loading…

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
JavaScript Minifier
Minify JavaScript by removing whitespace, comments, and shortening variable names to reduce file size
JavaScript Validator
Validate JavaScript syntax and find errors with detailed line numbers and error messages
JSON to JS Object
Convert JSON data to JavaScript object literal syntax with proper formatting and structure
JS Object to JSON
Convert JavaScript object literals to valid JSON format with proper escaping and structure
Console.log Remover
Strip console.log, warn, error, info and debug statements from JavaScript without touching strings, comments or regex
JS Comment Remover
Remove line and block comments from JavaScript while preserving URLs and comment syntax inside strings, with optional JSDoc keep

← View all JavaScript ToolsBrowse all 1,100+ tools →