[Home] > Snippets  > Languages  > JavaScript  > Arrays  >  Remove falsy values from array

Remove falsy values from array

JavaScript

const removeFalsy = (arr) => arr.filter(Boolean)

TypeScript

const removeFalsy = <T,_>(arr: T[]): T[] => arr.filter(Boolean)

Example

removeFalsy([0, 'a string', '', NaN, true, 5, undefined, 'another string', false])
// ['a string', true, 5, 'another string']