How to check if all array values are blank in Javascript

I have an array like:

["", "", "", "1", "", ""]

I want to alert when all the array values are blanks, i.e, when the array is like this:

["", "", "", "", "", ""]

How can I achieve this.


Solution 1:

Try this,

["", "", "", "", "", ""].join("").length==0

If you want to remove spaces,

["", "", "", "", "", ""].join("").replace(/\s/gi,'').length==0

Note :

This will not work for inputs like ["", [], "", null, undefined, ""]

Solution 2:

Use every():

const allEmpty = arr => arr.every(e => e === "");
console.log(allEmpty(["", "", "", "1", "", ""]));
console.log(allEmpty(["", "", "", "", "", ""]));