Categories
JavaScript Answers

How to filter null from an array in JavaScript?

Spread the love

To filter null from an array in JavaScript, we use the filter method.

For instance, we write

const filtered = [1, "", null, NaN, 2, undefined, 4, 5, 6].filter((x) => x);

to call [1, "", null, NaN, 2, undefined, 4, 5, 6].filter with a callback that returns the parameter to return an array that only includes the truthy values in [1, "", null, NaN, 2, undefined, 4, 5, 6].

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 *