Assuming the following variables:
const showFoo = false;
const showBar = true;
const arr = [];
if (showFoo) {
arr += "foo";
}
if (showBar) {
arr += "bar";
}
return arr;
// [...[]] === []
// [...[a], ...[b]] === [a, b]
return [
...(showFoo ? ["foo"] : []),
...(showBar ? ["bar"] : [])
];