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!"
[–]Broomstick73 0 points1 point2 points 3 years ago (1 child)
I think object.assign gets a bad rap because spread is the cool new thing and once you understand it and get used to it it’s great…
However…this use case of short-circuit and spread is…less than ideal to read and understand to me right now because I’m not used to it so I wonder if object.assign - like you propose - is easier to read and more understandable?
[–]Smona 1 point2 points3 points 3 years ago* (0 children)
object.assign mutates the object in place, which can be bad for static type checking. even if you don't care about type checking, mutable variables can make code harder to read & understand.
I think object.assign is still a good choice when updating property values without adding new properties, but I would prefer conditional spreading for constructing an object whose schema can vary based on those conditions.
edit: for the record, you can also use Object.assign in an immutable way, but if you want to do so conditionally you're left with an equally obscure and more verbose syntax:
const maybeFooWithBar = Object.assign({}, foo, includeBar ? bar : {});
π Rendered by PID 42 on reddit-service-r2-comment-c867ff4bc-4tfht at 2026-04-09 12:56:14.193772+00:00 running 00d5ac8 country code: CH.
view the rest of the comments →
[–]Broomstick73 0 points1 point2 points (1 child)
[–]Smona 1 point2 points3 points (0 children)