简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM