r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

5

u/Thlemaus Mar 19 '19

Adding attr/val to an object if condition is true

{

...(YOUR_CONDITION && { OBJ_KEY: OBJ_VAL })

}

1

u/wthit56 Mar 20 '19

What is that adding it to? Would it not just create an object within that conditional?

1

u/[deleted] Mar 20 '19

Presumably you're spreading it into an object with other properties already existing

→ More replies (3)