Conditionally add properties to objects in JavaScript

This comes in handy when you want to completely omit a property from an object in case the value is undefined or null. 

```js
{prop1, prop2, ...(condition && prop3)}
```

The trick being here: 

```js
...(condition && prop3)
```

In case the ```condition``` evaluates to false, the property will be completely omitted from the object, otherwise, both the value and its corresponding key will show up in the end result.

Nick Ciolpan
24 Oct 2021
« Back to post