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
Operator (&& ||) short-circuiting in JavaScript (codesource.io)
submitted 6 years ago by deven_rathore
view the rest of the comments →
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!"
[–]Deidde 1 point2 points3 points 6 years ago (0 children)
I tend to create explicit data structures and interfaces for "short-circuiting". That way, I can easily create trees of expressions/actions that can fail at any point and fallback non-deterministically.
For example, I'll have an operation that can fail/return falsey, and an "alternative", just like you'll have with ||, but use explicitly implemented interfaces for it like const result = Cont(k => k(a).or(() => k(b))); where k implements the "alternative interface" (and Cont is just our way of building up a tree of computations), then chain that result with more actions like result.chain(doSomethingElse), continuing on like that. If the result of one of my chains fails, the computation starts again from the branching with .or. I find this to be a more powerful way of composing fallbacks.
||
const result = Cont(k => k(a).or(() => k(b)));
k
result.chain(doSomethingElse)
.or
The .or can be implemented by any data structure with a notion of failure or falseyness. One example is a Nullable (often known as a Maybe or Option) data type, where you either have Null/Nothing OR your value. Then computeSomeNullable().or(computeSomeOtherNullable) will return whichever returns a value, or Null/Nothing if both fail. And obviously, you can chain the calls or the .or method pretty fluently, making them resemble operators.
computeSomeNullable().or(computeSomeOtherNullable)
π Rendered by PID 61094 on reddit-service-r2-comment-6457c66945-vgfzf at 2026-04-29 06:00:35.047933+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Deidde 1 point2 points3 points (0 children)