3 Ways to Remove an Empty String from an Array in JavaScript?

In today’s tutorial, we will hand you the three (3) effective solutions to remove empty strings from an array in JavaScript.

So bear with us as we explore different methods that help you to remove the empty string.

Are you ready? Well, let’s get started!

How to remove an empty string from an array in JavaScript?

To remove empty strings from an array in JavaScript, various approaches can be used.

Here are a few methods you might consider:

Solution 1: Use the filter() method and a truthy check

You can remove an empty string from an array using the filter() method and a truthy check.

Here’s an example:

const SampleArray = ['Programming', '', 'Software Engineering', '', 'Web Development'];

const result = SampleArray.filter(Boolean); ✅

console.log(result);

As you can see, we use the filter() function with the Boolean function as the filtering criteria.

Since an empty string is considered a falsy value, it will be removed from the resulting array.

Output:

[ 'Programming', 'Software Engineering', 'Web Development' ]

Solution 2: Iterating over the array using a for loop

This method iterates over the array using a for loop and checks each element for an empty string. Only non-empty strings are added to the filteredArray.

Here’s an example:

const SampleArray = ['English', '', 'Math', '', 'Science'];

const filteredArray = [];
for (let i = 0; i < SampleArray.length; i++) { ✅
  if (SampleArray[i] !== '') {
    filteredArray.push(SampleArray[i]);
  }
}

console.log(filteredArray);

Output:

[ 'English', 'Math', 'Science' ]

Solution 3: Use the reduce() method

You can also use the reduce() method to remove an empty string from an array in JavaScript.

Here’s an example:

const SampleArray = ['Software Engineering', '', 'Programming', '', 'Math'];

const filteredArray = SampleArray.reduce((acc, current) => {✅
if (current !== '') {
acc.push(current);
}
return acc;
}, []);

console.log(filteredArray);

This method uses the reduce() function to build the filteredArray. It checks each element for an empty string and accumulates the non-empty strings in the acc parameter.

Output:

[ 'Software Engineering', 'Programming', 'Math' ]

Conclusion

This article explores the three (3) effective solutions for removing empty strings from an array in JavaScript.

We’ve illustrated how the filter() method can be used with a truthy check or an explicit check for empty strings.

We’ve also looked at how a traditional for loop can be used to iterate over the array and push non-empty strings to a new array.

Each of these methods has its own advantages and use cases, and they all provide a way to clean up your arrays in JavaScript.

Remember that all these methods return a new array and do not modify the original array. If you want to update the original array, you can assign the result of these methods back to the original array.

We hope this article has provided you with enough information to understand the Javascript remove empty string from array.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment