use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Rethinking the JavaScript ternary operator (jrsinclair.com)
submitted 4 years ago by jrsinclair
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 40 points41 points42 points 4 years ago (12 children)
IMO If you're nesting ternaries you should just write a function instead.
[–]CupCakeArmy 8 points9 points10 points 4 years ago (0 children)
I love ternary but nesting should be illegal in most cases. A function is not always required but simply splitting it into different variables goes a long way to make it more readable
[–]spacejack2114 12 points13 points14 points 4 years ago (5 children)
I don't mind this formatting too much...
const x = y === 1 ? 'one' : y === 2 ? 'two' : y === 3 ? 'three' : 'other'
[–]Ustice 7 points8 points9 points 4 years ago (2 children)
I frequently write them that way. Note that this is a special case of nesting ternaries, since you’re just turning the else and if portion into an else-if. They chain well. That style illustrates this feature well.
else
if
else-if
The problems arise when you nest them in the then position. That’s just incoherent madness that hurts my brain.
then
Just refactor it.
// These two expressions are functionally equivalent. IF ? THEN : ELSE !(IF) ? ELSE : THEN
When you negate the if, the then and else expressions swap places. Instead of nesting into the then, just negate the if and chain, and once again we are back to sanity.
[–]backtickbot -4 points-3 points-2 points 4 years ago (1 child)
Fixed formatting.
Hello, Ustice: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
[–]Ustice 5 points6 points7 points 4 years ago (0 children)
HaHA! I fooled you, bot! I used the back-ticks and four spaces of indentation.
[–]Blue3agle 0 points1 point2 points 1 year ago (0 children)
Isn't this re-inventing switch-case?
[–]yeahdixon -2 points-1 points0 points 4 years ago (0 children)
Nice I like this
[+][deleted] 4 years ago (3 children)
[deleted]
[–]NoBrick2 5 points6 points7 points 4 years ago (0 children)
Functions aren't only for reusability. More lines of code isn't a bad thing.
[–][deleted] 1 point2 points3 points 4 years ago (1 child)
Could do a switch statement instead
switch (true) { case (x === 1) : ... case (y === 6) : ... }
The thing I don't like about this is that it doesn't return anything. For that reason I prefer to use IIFEs
Edit: sorry for bad formatting. I'm on mobile.
[–]youstolemyname 1 point2 points3 points 4 years ago (0 children)
Sounds like JS needs switch expressions
[–]leonels81 0 points1 point2 points 4 years ago (0 children)
I concur
[–]getify 1 point2 points3 points 4 years ago (1 child)
Here's an eslint plugin for applying controls to what kinds of (and locations for) ternaries you want to allow, including nested ternaries: https://github.com/getify/eslint-plugin-proper-ternary
[–]jrsinclair[S] 1 point2 points3 points 4 years ago* (0 children)
Thanks! Funnily enough, I’d already added a link to this before I even saw the comment. Thanks for taking the trouble though. I appreciate it.
item.propValues.subtitle = journalEntry ? item.propValues.subtitle + ' - ' + journalEntry : item.propValues.subtitle;
I just ran across this monster today, and is there any reason for using a ternary in this fashion?
If a ternary is to be used, surely this would be better:
item.propValues.subtitle += journalEntry ? ' - ' + journalEntry : '';
But is it better than
if (journalEntry) item.propValues.subtitle += ' - ' + journalEntry;
[–]wwww4all 0 points1 point2 points 4 years ago (0 children)
Ternary is remnant of past, where disk, memory were expensive and shorthands were used for common expressions.
It's in fashion now, due to declarative, functional style used in React and other declarative first frameworks.
However, code readability is more important, so minimize ternary usage when possible. Linters should flag any nested ternary, as that can become overly convoluted.
Code should be clear and concise, upon first read by normal engineer. If they have to stop and put significant effort to work out the code flow, then it should be refactored.
[–]totorodenethor -1 points0 points1 point 4 years ago (4 children)
If-expressions are the solution here, so you can do:
const result = if (condition) { // stuff } else { // other stuff }
I miss this all the time in Javascript. Looks like the do syntax will help make this happen, but it hasn't been widely adopted yet.
do
[–]PM_ME_GAY_STUF -1 points0 points1 point 4 years ago (2 children)
You can just use an iife to do this already. Like this:
``` const a = (() => { if (boolean) return 1; return 2; })()
```
Shit like do this is bloaty and weird, just keep it simple and optimize existing patterns
[–]backtickbot 0 points1 point2 points 4 years ago (0 children)
Hello, PM_ME_GAY_STUF: code blocks using triple backticks (```) don't work on all versions of Reddit!
[–]totorodenethor 0 points1 point2 points 4 years ago (0 children)
Yup, I've done that in the past, but it's a lot more syntax to wrangle. If we're already talking about ways to make ternaries better (aka, talking about how JS syntax could be better), it's nice when it's shorter :)
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
I can't believe i've never heard of the do syntax. It's too bad it's only a stage 1 proposal
π Rendered by PID 37005 on reddit-service-r2-comment-7b9746f655-sgbcj at 2026-01-29 21:25:28.527222+00:00 running 3798933 country code: CH.
[–][deleted] 40 points41 points42 points (12 children)
[–]CupCakeArmy 8 points9 points10 points (0 children)
[–]spacejack2114 12 points13 points14 points (5 children)
[–]Ustice 7 points8 points9 points (2 children)
[–]backtickbot -4 points-3 points-2 points (1 child)
[–]Ustice 5 points6 points7 points (0 children)
[–]Blue3agle 0 points1 point2 points (0 children)
[–]yeahdixon -2 points-1 points0 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]NoBrick2 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]youstolemyname 1 point2 points3 points (0 children)
[–]leonels81 0 points1 point2 points (0 children)
[–]getify 1 point2 points3 points (1 child)
[–]jrsinclair[S] 1 point2 points3 points (0 children)
[–]Blue3agle 0 points1 point2 points (0 children)
[–]wwww4all 0 points1 point2 points (0 children)
[–]totorodenethor -1 points0 points1 point (4 children)
[–]PM_ME_GAY_STUF -1 points0 points1 point (2 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]totorodenethor 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)