Succinct/concise syntax for 'optional' object keys in ES6/ES7?
There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 = ...; } Is there a way to define the object all at once with both optional and required keys? You can use object spread to have an optional property. Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. You might need the babel transform to use it. let flag1 = true; let flag2 = false; const obj = { requiredKey1: 1, requiredKey2: 2, ...(flag1 && {