Check If An Array Contains An Empty String In JavaScript

To check if an array contains an empty string in JavaScript, you can use any of these methods –

  1. Use the includes() method – This method checks if an array contains a specified element.
  2. Use the indexOf() method – This method returns the first index at which a given element can be found in the array, or -1 if it is not present.
  3. Use the some() method – This method tests whether at least one element in the array passes the test implemented by the provided function.
  4. Use the every() method – This method tests whether all elements in the array pass the test implemented by the provided function.
  5. Use the filter() method – This creates a new array with all elements that pass the test implemented by the provided function.

Let’s discuss each of these methods in detail below –

check if an array contains an empty string in javascript

Array.includes() Method To Check If An Array Contains An Empty String

The includes() method is used to check if an array contains a specified element. The syntax of this method is –

array.includes(element, start)

where,

element – Required. The element to search for.

start – Optional. The position in this array at which to begin searching for target.

This method returns a Boolean value – true if the element is found in the array, and false if it is not.

Therefore, to check if an array contains an empty string in JavaScript, you can use the includes() method as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.includes(“”)); // true

console.log(arr.includes(” “, 0)); // false

If you want to check if an array contains an empty string at a specific index, then you can use the start parameter as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.includes(“”, 1)); // true

Note that, if the start parameter is greater than or equal to the length of the array, then this method will always return false –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.includes(“”, 3)); // false

Array.indexOf() Method To Check If An Array Contains An Empty String

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. The syntax of this method is –

array.indexOf(element, start)

where,

element – Required. The element to search for.

start – Optional. The position in this array at which to begin searching for target.

This method returns the index of the first occurrence of the search element; otherwise, it returns -1.

Therefore, to check if an array contains an empty string in JavaScript using indexOf(), you can do as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.indexOf(“”)); // 1

If you want to check if an array contains an empty string at a specific index, then you can use the start parameter as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.indexOf(“”, 2)); // -1

Array.some() Method To Check If An Array Contains An Empty String

The some() method tests whether at least one element in the array passes the test implemented by the provided function. The syntax of this method is –

array.some(callback, thisArg)

where,

callback – Required. A function to be run for each element in the array.

thisArg – Optional. A value to use as this when executing callback.

This method returns a Boolean value – true if at least one element passes the test; otherwise, it returns false.

Therefore, to check if an array contains an empty string in JavaScript using some(), you can do as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.some(function (str) {

   if(str===”) return true;

   return false;

})); // true

Alternatively, you can use an arrow function as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.some((str) => str===””)); //true

Array.every() Method To Check If An Array Contains An Empty String

The every() method tests whether all elements in the array pass the test implemented by the provided function. The syntax of this method is –

array.every(callback, thisArg)

where,

callback – Required. A function to be run for each element in the array.

thisArg – Optional. A value to use as this when executing callback.

This method returns a Boolean value – true if every element passes the test; otherwise, it returns false.

Therefore, to check if an array contains an empty string in JavaScript using every(), you can do as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.every(function (str) {

   if(str===””) return true;

   return false;

})); // false

Alternatively, you can use an arrow function as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

console.log(arr.every((str) => str===””)); //false

forEach Method To Check If An Array Contains An Empty String

The forEach() method executes a provided function once for each array element. The syntax of this method is –

array.forEach(callback, thisArg)

where,

callback – Required. A function to be run for each element in the array.

thisArg – Optional. A value to use as this when executing callback.

The callback function accepts three parameters –

value – The value of the current element being processed in the array. (Required)

index – The index of the current element being processed in the array. (Optional)

array – The array that forEach() was called upon. (Optional)

Therefore, to check if an array contains an empty string in JavaScript using forEach(), you can do as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

arr.forEach(function (str, index, arr) {

  if(str===””) {

    console.log(“array contains an empty string at index “+index);

  }

}); // array contains an empty string at index 1

Alternatively, you can use an arrow function as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

arr.forEach((str, index, arr) => {

   if(str===””) {

     console.log(“array contains an empty string at index “+index);

   }

}); // array contains an empty string at index 1

filter Method To Check If An Array Contains An Empty String

The filter() method creates a new array with all elements that pass the test implemented by the provided function. The syntax of this method is –

array.filter(callback, thisArg)

where,

callback – Required. A function to be run for each element in the array.

thisArg– Optional. A value to use as this when executing callback.

This method returns a new filtered array.

Therefore, to check if an array contains an empty string in JavaScript using filter(), you can do as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

var newArr = arr.filter(function (str, index) {

   return str===””;

})

if(newArr.length > 0) {

   console.log(“array contains an empty string”);

} else {

   console.log(“array does not contain any empty string”);

} // array contains an empty string

Alternatively, you can use an arrow function as follows –

var arr = [“JavaScript”, “”, “Tutorial”];

var newArr = arr.filter((str, index) => str===””)

if(newArr.length > 0) {

  console.log(“array contains an empty string”);

} else {

  console.log(“array does not contain any empty string”);

} // array contains an empty string

Conclusion

In this tutorial, you have learned how to check if an array contains an empty string in JavaScript using several methods. You can use any of these methods as per your requirements.

Leave a Comment