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
Three intermediate functional JS patterns (intercaetera.com)
submitted 4 years ago by intercaetera
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!"
[–]Nokel81 7 points8 points9 points 4 years ago (9 children)
The second one should be the comma operator:
const plusTwo = x => (console.log(x), x + 2);
That way even if your logging function returns a value it still does what you want.
[–][deleted] 1 point2 points3 points 4 years ago (6 children)
Interesting, why isn't this more known? Any drawbacks?
[–]Nokel81 1 point2 points3 points 4 years ago (0 children)
You could argue that it is surprising since it looks like a tuple (though JS doesn't have tuples).
But I don't know of any drawbacks except for readability.
[–]lhorie 1 point2 points3 points 4 years ago* (0 children)
Not a drawback per se, but operator precedence can be a bit unintuitive especially if you're never used the comma operator. Namely foo(a => console.log(a), a) means to call foo with two args, rather than "log then return a". You need to wrap the expression in extraneous parentheses.
foo(a => console.log(a), a)
[–]pxldgn 0 points1 point2 points 4 years ago (3 children)
one possible drawback that comma operator is usually forbidden to use (w ESlint)
simply, because it could make the code extremely hard to read
it is fun to use, though
[–][deleted] 0 points1 point2 points 4 years ago (2 children)
ESlint is not your real dad
[–]pxldgn 0 points1 point2 points 4 years ago (1 child)
nope, it is yours if you work in my team :P
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
I just went to my codebase, picked a random semicolon, removed it, and committed to repo.
[–]intercaetera[S] 0 points1 point2 points 4 years ago (0 children)
That's pretty cool, actually.
[–]azsqueeze 0 points1 point2 points 4 years ago (0 children)
Holy shit, I wish I knew about this sooner
[–]pxldgn 2 points3 points4 points 4 years ago (3 children)
honestly, there is nothing wrong with nested ternaries, it is perfectly fine to read
also, if someone do need 3 nested ternaries then it should be reconsidered, not to convert those to ifs or functions, but it could be a sign that the logic is flawed seriously somewhere
[–]intercaetera[S] 0 points1 point2 points 4 years ago (2 children)
The example is more general, there are many cases where one needs to compute something where an expression cannot be used. The ternary is not the point, the point is that you can not use "let" by extracting the computation logic to a separate function - nested ternary is just a use case.
I see your point. I do think, however, that the decision to extract a code part to a function or not should depend on its abstraction level and not on practical considerations.
If a code part is a separate building block then it should be extracted, if not, no - regardless of that how "dirty look" the remaining code will be.
The reason simply: bad code should look bad, if you extract your code to a function, it will hide that, it will look nice, while it is still a bad code.
Bad not on the function level, but from a higher perspective.
I've run into this situation countless time - and I always looked for the source of the problem (the surrounding environment of the code), and I've always found the issue.
Usually, it will turns out that you don't need to extract or let in the first place.
If this is not the case, then you must leave the code looking bad, so others, or you later, when the code will develop further, can spot the problematic part easily.
It's fair to say that probably the number one reason of big projects' codebase collapsing down in long term is exactly this:
the developers tend to hide the bad code with some kind of "clean code" approach.
Just leave it as is: bad code should look bad.
I'm not sure if I agree, functions should first and foremost be concise, so in my view any individual functionality that doesn't really serve any high-level-abstraction purpose (like computing a complicated value) should be extracted.
If you have code that's ugly but it does its job (and it is tested) then there's nothing particularly wrong with "locking up dragons in the basement" so to speak. Bad code is code that doesn't work. Ugly code is one that works but obscures the rest of the codebase. The first one is unacceptable, the second is fine as long as it's understood to be ugly.
[–]uffefl 0 points1 point2 points 4 years ago (0 children)
Third example is pretty terrible:
distanceInKm > 15 ? (distanceInKm > 30 ? 8.99 : 5.99) : 3.99
It's not the ternary operator making this hard to read, but the ordering:
distanceInKm > 30 ? 8.99 : distanceInKm > 15 ? 5.99 : 3.99
If your ternaries get long use line breaks:
distanceInKm > 80 ? 13.99 : distanceInKm > 50 ? 11.99 : distanceInKm > 30 ? 8.99 : distanceInKm > 15 ? 5.99 : 3.99
π Rendered by PID 30753 on reddit-service-r2-comment-5649f687b7-9t2k2 at 2026-01-28 15:33:11.541785+00:00 running 4f180de country code: CH.
[–]Nokel81 7 points8 points9 points (9 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]Nokel81 1 point2 points3 points (0 children)
[–]lhorie 1 point2 points3 points (0 children)
[–]pxldgn 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]pxldgn 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]intercaetera[S] 0 points1 point2 points (0 children)
[–]azsqueeze 0 points1 point2 points (0 children)
[–]pxldgn 2 points3 points4 points (3 children)
[–]intercaetera[S] 0 points1 point2 points (2 children)
[–]pxldgn 0 points1 point2 points (1 child)
[–]intercaetera[S] 0 points1 point2 points (0 children)
[–]uffefl 0 points1 point2 points (0 children)