7
4815

How to Check if Array Contains Empty String in JavaScript

Reading Time: 3 minutes

There are 5 ways to easily check if Array contains empty string in JavaScript where most of them are 1 liner code. Let’s explore the methods.

Methods to Check if Array contains Empty String in JavaScript

Here are the list of 5 methods where we can easily check if array contains empty string. Since each of them have their own properties and features, we can choose which to use depending on our needs.

  • Array includes() Method
  • Array indexOf() Method
  • Array some() Method
  • Array filter() Method
  • Set has() Method

Let’s look into each details with illustration below.

1. Array includes() Method

includes() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Check if the array itself contain certain value
  • Allow specifying the index to start from
  • Return true if the value exist
  • Does not modify the array

Below shows the example where we send an array with and without empty string to includes() method.

let myArray = ['one', 'two', 'three'];
console.log(myArray.includes(''));  // false

myArray = ['one', 'two', 'three', ''];
console.log(myArray.includes(''));  // true

myArray = ['', 'one', 'two', 'three'];
console.log(myArray.includes('', 3));  // false

2. Array indexOf() Method

indexOf() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Check if the array itself contain certain value
  • Return the first index if the element is found
  • Does not modify the array

Below shows the example where we send an array with and without empty string to indexOf() method. If the empty string is found, it return the first index. Thus, if it does not return -1, empty string is found.

let myArray = ['one', 'two', 'three'];
console.log(myArray.indexOf(''));  // -1

myArray = ['one', 'two', 'three', ''];
console.log(myArray.indexOf(''));  // 3

3. Array some() Method

some() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Allow specifying of test to execute on every element
  • Return true immediately for first value to pass the test
  • Does not modify the array

Below shows the example where we define and arrow function that check if string is empty. Then, we pass this function as the test into some() method.

let myArray = ['one', 'two', 'three'];

const containEmptyString = (element) => element === '';
console.log(myArray.some(containEmptyString));  // false

myArray = ['one', 'two', 'three', ''];
console.log(myArray.some(containEmptyString));  // true

4. Array filter() Method

filter() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Allow specifying of test to execute on every element
  • Return an array of elements that pass the test
  • Does not modify the array

Below shows the example where we define and arrow function that check if string is empty. Then, we pass this function as the test into filter() method. If there is no empty string, we will receive empty array.

let myArray = ['one', 'two', 'three'];

const containEmptyString = (element) => element === '';
console.log(myArray.filter(containEmptyString));  // []

myArray = ['one', 'two', 'three', ''];
console.log(myArray.filter(containEmptyString));  // [""]

5. Set has() Method

Set is a standard built-in object in JavaScript. By converting an Array into Set, we can use its has() method to easily determine if an empty string exist.

Its features includes:

  • Return true if the Set contain that element with that datatype
  • Does not modify the array

Below shows the example where we define and arrow function that check if string is empty. Then, we pass this function as the test into filter() method. If there is no empty string, we will receive empty array.

let myArray = ['one', 'two', 'three'];
let mySet = new Set(myArray);
console.log(mySet.has(''));  // false

myArray = ['one', 'two', 'three', ''];
mySet = new Set(myArray);
console.log(mySet.has(''));  // true

Conclusion

We have look into 5 different methods to check if there is an empty string in an array.

If we do not need to know about the position of empty string, Array includes() and some() as they will just return a boolean result. Set has() method also returns a boolean and it maybe more efficient for processing huge array.

If we need to know about the position, array indexOf() will be useful since we can immediately perform processing.

Show Comments

No Responses Yet

Leave a Reply