Filtering falsy values

How to quickly discard falsy values from an array

March 22, 2017 - 1 minute read -

How to quickly discard falsy values from an array? Thanks to Array.filter(Boolean), this is an easy task.

> ['this', 'has', undefined, 
  false, 0, null, NaN, 'values'].filter(Boolean)

[ 'this', 'has', 'values' ]

This works because Boolean(val) is a function that coerces a value into a Boolean.