How to remove falsey values from an array

#2

JS

July 13, 2019

When dealing with arrays in Javascript, it's not uncommon to have to remove some falsey values - 0, null, NaN, ' ', undefined, false - from them. One of the most practical ways is to user .filter() and pass a function that will remove whatever type of these falsey values you need to get rid of.

var arr = [1, 2, 10, 0, false, undefined, null, '', 'back-end', 'front-end', 'fullstack'] var filteredArr = arr.filter(Boolean) console.log(arr) console.log(filteredArr)

But, actually, the easiest way to get this done is simply by using .filter(Boolean) and all your falsey values will be removed just like in the example. This works because 'Boolean' is an object wrapper to whom who we are passing each element of the array. It will return 'true' if it's a truthy value and 'false' if it's a falsy one thus passing this boolean to the filter method. Hope it's handy for you! 👌