Categories
JavaScript Answers

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

Spread the love

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

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

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

To remove empty strings from array while keeping record without loops with JavaScript, we call the filter method.

For instance, we write

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

to call arr.filter with Boolean to return a new array without the falsy values in arr.

Empty string is a falsy value so it’s filtered out.

Conclusion

To remove empty strings from array while keeping record without loops with JavaScript, we call the 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 *