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]
.