Skip to content

Instantly share code, notes, and snippets.

@mijimoco
Created January 30, 2017 06:46
Show Gist options
  • Save mijimoco/9128355d48673f37417b633d2db5a582 to your computer and use it in GitHub Desktop.
Save mijimoco/9128355d48673f37417b633d2db5a582 to your computer and use it in GitHub Desktop.
Falsy Bouncer
function bouncer (arr) {
var bouncedArr = arr.filter(Boolean);
console.log(bouncedArr);
return bouncedArr;
}
bouncer([7, "ate", "", false, 9]);
@mijimoco
Copy link
Author

Boolean is an object wrapper for boolean values. It's not crystal clear to me what the F that means, but it's very useful. Why? Because as the documentation says "If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false", which is great, as that's exactly what we need inside our filter. We want items that do not fulfill our filter, or false items will be filtered out. That's why arr.filter(Boolean); gets rid of all Nan undefined or "" items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment