DEV Community

Discussion on: Spread Syntax "Three-dots" Tricks You Can Use Now

Collapse
 
robincsamuel profile image
Robin C Samuel

One other reason I use spread is to conditionally add a key to an object,

const user = {
     firstName: input.firstName,
     ...(input.lastName ? { lastName: input.lastName } : null)
}
Collapse
 
miggu profile image
miggu • Edited

I use this ALL THE TIME, you can use an empty object {} as well instead of null

Collapse
 
bashunaimiroy profile image
Bashu Naimi-Roy

I might end up using this, thanks Robin!