Conditionally adding properties inside object literals is a bit of a pain in javascript. Alas, with the advent of ES6, we get the amazing spread operator.


const conditional = false;
const obj = {
  ...(conditional ? {keyA: 1} : {}),
  keyB: 2,

// {keyB: 2}

This works because the spread operator expands object literals, expecting zero or more key-value pairs. If the object has no key-value pairs to enumerate over, it returns nothing.