Home » Javascript » Javascript boolean not returning all false value

Javascript boolean not returning all false value

Posted by: admin November 1, 2017 Leave a comment

Questions:

Can someone please explain what’s wrong with the code below?

Goal of the function below: Remove all falsy values from an array.

function bouncer(arr) {
     var newArray = arr.filter(function(element){
     return element != Boolean();
  });
  return newArray;
}
bouncer([7, "ate", "", false, 9, null, false, 0, NaN, undefined]);

It works but it only works for “false” and “0”. I understand that I am overcomplicating myself by doing this when I can simply just use the code below.

function bouncer(arr) {
     var newArray = arr.filter(Boolean);
  return newArray;
}
Answers: