How can I check JavaScript arrays for empty strings?

Viewed 44360

I need to check if array contains at least one empty elements. If any of the one element is empty then it will return false.

Example:

var my_arr = new Array(); 
my_arr[0] = ""; 
my_arr[1] = " hi ";
my_arr[2] = "";

The 0th and 2nd array elements are "empty".

16 Answers

Using a "higher order function" like filter instead of looping can sometimes make for faster, safer, and more readable code. Here, you could filter the array to remove items that are not the empty string, then check the length of the resultant array.

Basic JavaScript

var my_arr = ["", "hi", ""]

// only keep items that are the empty string
new_arr = my_arr.filter(function(item) {
  return item === ""
})

// if filtered array is not empty, there are empty strings
console.log(new_arr);
console.log(new_arr.length === 0);

Modern Javascript: One-liner

var my_arr = ["", "hi", ""]
var result = my_arr.filter(item => item === "").length === 0
console.log(result);

A note about performance

Looping is likely faster in this case, since you can stop looping as soon as you find an empty string. I might still choose to use filter for code succinctness and readability, but either strategy is defensible.

If you needed to loop over all the elements in the array, however-- perhaps to check if every item is the empty string-- filter would likely be much faster than a for loop!

Nowadays we can use Array.includes

my_arr.includes("")

Returns a Boolean

my_arr.includes("")

This returned undefined instead of a boolean value so here's an alternative.

function checkEmptyString(item){
     if (item.trim().length > 0) return false;
     else return true;
    };
    
function checkIfArrayContainsEmptyString(array) {
  const containsEmptyString = array.some(checkEmptyString);
  return containsEmptyString;
};
    
console.log(checkIfArrayContainsEmptyString(["","hey","","this","is","my","solution"]))
// *returns true*

console.log(checkIfArrayContainsEmptyString(["yay","it","works"]))
// *returns false*

yourArray.join('').length > 0

Join your array without any space in between and check for its length. If the length, turns out to be greater than zero that means array was not empty. If length is less than or equal to zero, then array was empty.

One line solution to check if string have empty element

let emptyStrings = strArray.filter(str => str.trim().length <= 0);

let strArray = ['str1', '', 'str2', ' ', 'str3', '    ']
let emptyStrings = strArray.filter(str => str.trim().length <= 0);
console.log(emptyStrings)

One line solution to get non-empty strings from an array

let nonEmptyStrings = strArray.filter(str => str.trim().length > 0);

let strArray = ['str1', '', 'str2', ' ', 'str3', '    ']
let nonEmptyStrings = strArray.filter(str => str.trim().length > 0);
console.log(nonEmptyStrings)

If you only care about empty strings then this will do it:

const arr = ["hi","hello","","jj"]
('' in arr) //returns false

the last line checks if an empty string was found in the array.

I don't know if this is the most performant way, but here's a one liner in ES2015+:

// true if not empty strings
// false if there are empty strings
my_arr.filter(x => x).length === my_arr.length

The .filter(x => x) will return all the elements of the array that are not empty nor undefined. You then compare the length of the original array. If they are different, that means that the array contains empty strings.

array.includes("") works just fine.

Let a = ["content1", "" , "content2"]; console.log(a.includes(""));

//Output in console true

Related