> For the complete documentation index, see [llms.txt](https://tricks.bodik.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tricks.bodik.tech/programming/javascript/how-to-add-a-property-to-javascript-object-if-it-is-not-empty-or-null.md).

# How to add a property to JavaScript object if it is not empty or null?

If you need to add some property to your JavaScript object using elegant one line solution, you can use this approach.

```javascript
let someObj = { prop1: 1 };
let prop2 = null;
let prop3 = 3;

someObj = { ...someObj, ...(prop2 && { prop2 }), ...(prop3 && { prop3 }) };

console.log(someObj);
//{ prop1: 1, prop3: 3 }
```
