r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

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

1

u/wthit56 Mar 20 '19

Interesting. What would that code look like?

2

u/Thlemaus Mar 21 '19

const test = {attr: true};

const obj = {

title: 'test',

...(test.attr && {valid: 'yes'})

}

console.log(obj); // Object { title: "test", valid: "yes" }

1

u/wthit56 Mar 21 '19

Oh, I see! That’s cool. I didn’t know you could do that 👍