You can conditionally add JavaScript array elements in a single expression
You can conditionally add JavaScript array elements in a single expression.
How? Use spread:
const arr = [
...(includeA ? ['a'] : []),
'b',
];
This only includes a in the array if includeA is true. This works because spread ignores empty arrays. 😎
#javascript
Further reading in (yet another) great concise post by @2ality: https://2ality.com/2017/04/conditional-literal-entries.html