Filter null from an array in JavaScript

Well, since all of your values are falsy, just do a !! (cast to boolean) check:

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

Edit: Apparently the cast isn’t needed:

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

And the code above removes "", null, undefined and NaN just fine.

Leave a Comment