Heggy Castaneda
Aug 4, 2017 · 2 min read

Falsy Bouncer

Remove all falsy values from an array.

Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

Here are some helpful links:

First approach using !!:

function bouncer(arr){
// filter function returns new filtered array
return arr.filter(function(el){
// filters in only elements that are truthy
return !!(el);
});
}
bouncer([7, "ate", "", false, 9]);

Option 2) Longer with using Boolean

https://repl.it/Jwou/1
function bouncer(arr) {
// temp holding pot where filtered array will reside
var result = [];
// result gets new filtered array with the elements
// that pass the test Boolean(element)
// pass element to get access to the original array
result = arr.filter(function(element){
// only element in orig. arr that pass truthy test
// will be push into result arr
return Boolean(element);
});
return result;
}

Lastly, FCC official solution.

https://repl.it/Jwou/2
function bouncer (arr) {
// pass in Boolean funtion as callback into filter
// each element goes through Boolean function.
// elements that pass Boolean test (truthy)
// gets returned.
return arr.filter(Boolean);
}
bouncer([7, "ate", "", false, 9]);

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade