Skip to content

Falsy Bouncer

Islam Ibakaev edited this page Apr 26, 2016 · 1 revision

Details

Remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

Elegant solution

function bouncer(arr) {
  return arr.filter(function(el) {return el;});
}

bouncer([7, "ate", "", false, 9]); // returns [7, 'ate', 9]
Clone this wiki locally