I am trying type check for undefined in the first part of a ternary
return typeof this.scores != 'undefined' ? this.scores.filter()
and
return (typeof this.scores != 'undefined') ? this.scores.filter()
Do I have to use a full if/else?
What you have will work if you finish the ternary expression:
return typeof this.scores != 'undefined' ? this.scores.filter() : null;
You can replace null with whatever value you want to return instead.
You could return an undefined or the value of the function call by using a logical AND && instead of a conditional (ternary) operator ?: without an else part (which is not possible).
return this.scores && this.scores.filter();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With