Categories
JavaScript Answers

How to remove empty strings from array while keeping record without a loop with JavaScript?

Spread the love

Sometimes, we want to remove empty strings from array while keeping record without a loop with JavaScript.

In this article, we’ll look at how to remove empty strings from array while keeping record without a loop with JavaScript.

How to remove empty strings from array while keeping record without a loop with JavaScript?

To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter method.

For instance, we write

const arr = ["I", "am", "", "still", "here", "", "man"];
const filteredArr = arr.filter(Boolean);

to call arr.filter with Boolean to return an array with the falsy values filtered out.

Since empty strings are falsy, they won’t be included in the returned array.

Conclusion

To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *