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
Methodincludes
() - 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.
s()include
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
method.
()include
s
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
is part of original Array standard built-in object method in JavaScript.
()indexOf
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
method. If the empty string is found, it return the first index. Thus, if it does not return -1, empty string is found.
()indexOf
let myArray = ['one', 'two', 'three']; console.log(myArray.indexOf('')); // -1 myArray = ['one', 'two', 'three', '']; console.log(myArray.indexOf('')); // 3
3. Array some()
Method
is part of original Array standard built-in object method in JavaScript.
()some
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
is part of original Array standard built-in object method in JavaScript.
()filter
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
is a standard built-in object in JavaScript. By converting an Set
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.
No Responses Yet