cay cospete
cay cospete

Reputation: 33

Why i'm getting undefined using Boolean object?

function boo(arr)
{
for(var i=0;i<arr.length;i++)
 { 
   var bob = new Boolean(arr[i]);
   if(bob === false)
    {

     console.log(arr[i]);   

    }

 }
}
console.log(boo([0,"how",89,false]));

I want to check all the false values like false,null,undefined etc.from an array using Boolean Object and print it. How can i do it using Boolean Object?. How can i add multiple values for checking whether it is true or false in Boolean Object?

Upvotes: 0

Views: 869

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 191056

You can't compare Boolean objects that way in javascript. new Boolean(...) === false will always evaluate to false.

MDN has a good warning on the matter:

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

For instance (given value = arr[i]), if you need to see if something is truthy just use !!value. For falsey, use !value. If you need to see if something is really true use value === true, likewise for false, but not a Boolean instance.

Upvotes: 2

Related Questions