'Expansion with conditional ternary operator
Simple question: Is there a way to use an expansion inside a conditional statement using the ternary operator?
const a = 1, b = 2;
// Works
console.log(...[ a, b ]);
// Works
console.log(...(a ? [ a, b ] : [ 'Not found' ]));
// Doesn't work
console.log(a ? ...[ a, b ] : 'Not found');
Solution 1:[1]
Yes and No
No, because in some cases it is not supported
Yes, you need to use it in the way it supports, basically manipulate it
For example
const a = 1, b = 2;
// Works
console.log(...[ a, b ]);
// Doesn't work
//console.log(a ? ...[ a, b ] : 'Not found');
//use this
a ? console.log(...[a,b]) : "Not Found"
//you can have other ways too to achieve this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TechnoTech |
