DEV Community

Discussion on: How not to sort an array in JavaScript

Collapse
 
pavelloz profile image
Paweł Kowalski

Nice one, didnt know that one :)

Thread Thread
 
philnash profile image
Phil Nash

They are both cool filter expressions! 😎

But... they both lose 0 or '' which you might not want. That's why I like the undefined check.

> const array = [-1, 0, 1]
[ -1, 0, 1 ]
> array.filter(Boolean)
[ -1, 1 ]
> array.filter(x => x)
[ -1, 1 ]