231412 412389421
231412 412389421

Reputation: 313

Check if all of array items are undefined or empty string

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

Answers (3)

Pointy
Pointy

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

Alexandre Senges
Alexandre Senges

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

CD..
CD..

Reputation: 74116

You can use every:

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Upvotes: 3

Related Questions