Is it possible to write two values ​if the condition in the ternary operator is not satisfied

Viewed 63

I have a ternary operator

let { a, b } = data ? getData(data ) : {'', ''};

Can I write two null values ​​into variables?

1 Answers

After the :, put an object where a and b properties are null:

let { a, b } = data ? getData(data ) : { a: null, b: null };

To be slightly less repetitive, you can use default values on the left-hand side of the =, if the default getData's object values won't be undefined:

let { a = null, b = null } = data ? getData(data ) : {};
Related