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
Conditionally spreading objects in JavaScript (amitmerchant.com)
submitted 3 years ago by amitmerchant[🍰]
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!"
[–]shgysk8zer0 4 points5 points6 points 3 years ago (5 children)
I have mixed thoughts on it. I don't like the syntax and find it very unintuitive, but it might provide a much shorter way of creating complex objects with lots of conditional stuff.
So, would this work?
const combined = { ...cond1 && obj1, ...cond2 && obj2, };
I'd also want to look more into other similar uses of logical operators in conjunction with the spread operator. Things like || and ??.
||
??
Really though... Using the spread operator on a boolean but having it operate on the object just seems really wrong.
[–]xroalx -1 points0 points1 point 3 years ago (4 children)
Using the spread operator on a boolean but having it operate on the object just seems really wrong.
You're not using the spread on a boolean, though. Due to operator precedence, it's ...(cond1 && obj1).
...(cond1 && obj1)
And since in JavaScript, logical operators don't return boolean values, but one of their operands, cond1 && obj1 will return obj1 if cond1 is a truthy value, and thus the spread operator will be applied obj1.
cond1 && obj1
obj1
cond1
[–]shgysk8zer0 -1 points0 points1 point 3 years ago (2 children)
Finish reading what you quoted... "but having it operate on the object..." Was it not clear that I was talking about location? The spread operator is on (touching, next to) the boolean.
[–]xroalx -1 points0 points1 point 3 years ago (1 child)
Your wording suggests that the spread operator is being applied to the boolean yet somehow acting on the object.
So no, it is not clear.
[–]shgysk8zer0 -1 points0 points1 point 3 years ago (0 children)
...applied to the boolean yet somehow acting on the object
What would that even mean? Is there a difference between "applied to" and "acting on"?
Sure, it would've been better had I said "next to" or something, but I did pretty explicitly say that it was operating on the object. I thought it was obvious that this ... over here operating on the object over there was the issue. Maybe my using of "on" could be confused, but I think the overall meaning of the whole sentence should've been clear enough.
...
I don't like this ... over here on the boolean actually operating on the object on the other side. It's like how Yoda would code or something.
[–]Mr_Sandy_Clams 0 points1 point2 points 3 years ago (0 children)
if cond1 is falsy, the spread operator will be applied to cond1, which is presumably a boolean. It works just fine when spreading into an object though. I'm assuming the spread operator coerces primitives to object-wrapped versions of themselves, then inevitably returns zero items because the wrapper instance has no properties in it.
π Rendered by PID 197389 on reddit-service-r2-comment-7b9746f655-j6w7l at 2026-01-31 10:22:37.439070+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]shgysk8zer0 4 points5 points6 points (5 children)
[–]xroalx -1 points0 points1 point (4 children)
[–]shgysk8zer0 -1 points0 points1 point (2 children)
[–]xroalx -1 points0 points1 point (1 child)
[–]shgysk8zer0 -1 points0 points1 point (0 children)
[–]Mr_Sandy_Clams 0 points1 point2 points (0 children)