Falsy Bouncer Boolean object

var x = new Boolean(false);
if (x) {
  // this code is executed
  console.log('Hi Airas');
}

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
 return console.log(arr.filter(function(x){
    var a = new Boolean(x);
    if(a){
      return x;
    }
  }));
}

bouncer([7, "ate", "", false, 9]);

Hey guys I am having some difficulty in understanding boolean objects that why even we use this “new Boolean()” thing. Also it was written in javascript documentation that “any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement”. So the first code is testing this statement and yeah this statement seems to be true.

Now second code is removing all falsy values from array but it is also contradicting the above statement which I highlited in inverted commas. Like when array’s false value will come in filter method it will be var a=new Boolean(false), now according to that statement when a passed to conditional statement it should return that false array value because code will be executed inside that conditional block and should return x which is false value. But instead it is not returning it, even the code is doing what it meant to but why it is contradicting that statement. I hope you guys understand my point here.

array.filter() creates an array with each element that returns true in your callback.
This

if (a) {
  return x;
}

gets executed because a is true for the reasons you mentioned, but the function actually returns false (the value of x) so the false element is not included in the new array.

Is that similar for ‘’, Nan ,null etc like all the falsy values.if return null in conditional statement it will also be evaluated as false ?

function bouncer(arr) {

   function truthy(value) {
   return value;
   }

   var filtered = arr.filter(truthy);
   return filtered;
 }

 bouncer([7, "ate", "", false, 9]);

Thats the reason even there is no Boolean function or object used still when truthy function runs ,return “” or any false value evaluates to be false thus filtering them out ? Please correct me if I am wrong here…

Your last post confused me a little bit. I assume you ask what’s the reason of having new Boolean() that returns true when we want to remove falsy values. The answer is that you are using it wrong. You shouldn’t be using it at all in this challenge to be honest but that’s not your fault, there was a link for it that confused you.

Keep in mind that new Boolean doesn’t return a boolean value, it returns a boolean object and in order to get the actual boolean value from it you have to use .valueOf() like:

// This actually returns false
new Boolean(false).valueOf();

new Boolean() is used for other stuff and not to get primitive values. If you want to convert a value to boolean, use Boolean(value) instead.

Yeah thanks I get that but what about my last question that if we return any falsy value it will be evaluated as false ? Like " return undefined= false"