Skip to main content

Remove falsy values from array

JavaScript version

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

TypeScript version

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

Comments