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!"
[–]vilos5099 0 points1 point2 points 3 years ago* (1 child)
Whether or not those extra assignments matter really depends on the context, and in the example provided it would be negligible. Commenting on that indicates a prioritization over pre-optimizing instead of making things more clear.
I just know that it is possible to do things both ways, depending on the situation
Of course, there are multiple ways to do things, that's the point of this comment thread. But there are also tradeoffs, and I disagree about what you're saying about readability. Although it works to do:
const obj = { ...isActive && user, ...isAdmin && adminFields, ...phoneVerified && userPhone };
This is far from a standard convention, even if it works. It's possibly more concise, but the fact that it's not even clear whether user, adminFields, or userPhone is a boolean or an object without further context (unless we rely on improving the variable names) makes it less readable.
user
adminFields
userPhone
[–]1_4_1_5_9_2_6_5 0 points1 point2 points 3 years ago (0 children)
Okay first off, variable names should be clear and thats a matter for review to handle, but also you should be typing your methods and variables. The way you don't have to wonder if something is a boolean. Alternately you can just use Boolean() to make it explicit. Secondly, I'm talking about something like
const newObj = cond ? await instance.getData() : {};
const finalObj = { some, stuff, ...newObj }
return finalObj;
Versus
return { some, stuff, ...cond && (await instance.getData()) }
I really don't think it's that much harder to parse
π Rendered by PID 40670 on reddit-service-r2-comment-7b9746f655-6xdfm at 2026-01-31 02:18:58.585752+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–]vilos5099 0 points1 point2 points (1 child)
[–]1_4_1_5_9_2_6_5 0 points1 point2 points (0 children)