Falsy Bouncer - What's wrong?

What’s the problem with this code?

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var arr2=arr.filter(function(item){
    return item!=false;
  } );
  return arr2;
bouncer([7, "ate", "", false, 9]);

When using the != operator, null != false evaluates to false. Same with NaN and undefined. So the new array retains these values.

You don’t have to compare item with false. The JS engine will handle coercing the output of the filter callback to a boolean value, so you just have to return item itself.

1 Like