Using a "higher order function" such as filter instead of loops can sometimes make code faster, safer, and more readable. Here you can filter the array to remove elements that are not an empty string, and then check the length of the resulting array.
Basic javascript
var my_arr = ["", "hi", ""]
Modern Javascript: Single Line
var my_arr = ["", "hi", ""] var result = my_arr.filter(item => item === "").length === 0 console.log(result);
Performance Note
Looping in this case is most likely faster, since you can stop looping as soon as you find an empty string. I could still use filter for code brevity and readability, but any strategy is justified.
If you need to iterate over all the elements in an array, perhaps however-- to check if each element is an empty string-- filter will be much faster than a for loop!
Dustin michels
source share