Categories
JavaScript Answers

How to remove empty array values from an array with JavaScript?

Spread the love

Sometimes, we want to remove empty array values from an array with JavaScript.

In this article, we’ll look at how to remove empty array values from an array with JavaScript.

How to remove empty array values from an array with JavaScript?

To remove empty array values from an array with JavaScript, we can call array’s filter method with Boolean.

For instance, we write:

const arr = ['One', 'Two', '', 'Four', '', ''];
const newArr = arr.filter(Boolean);
console.log(newArr);

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

Empty strings are falsy, so they’ll be filtered out.

Therefore, newArr is ["One", "Two", "Four"].

Conclusion

To remove empty array values from an array with JavaScript, we can call array’s filter method with Boolean.

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 *