Reputation: 313
How to check if all of items
are undefined
or empty string ''
?
var items = [variable_1, variable_2, variable_3];
Is there any nice way to do that instead of big if
? Not ES6.
Upvotes: 1
Views: 5465
Reputation: 413720
Here's another one I just thought of:
if (array.join("") === "")
// all undefined or ""
Note that that'll also be true
if an element is null
, not undefined
, so it may or may not suit the OP. Advantage is that it doesn't need a callback function.
Upvotes: 0
Reputation: 1589
A one liner that uses Array.prototype.some to find at least one element which is not undefined or '', if it finds one it returns true
!items.some(item => item != undefined || item != '')
Upvotes: 0