[英]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. 我想使用布尔对象从数组中检查所有错误值,例如false,null,undefined等,并将其打印出来。 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? 如何在布尔对象中添加多个值以检查它是
true
还是false
?
You can't compare Boolean
objects that way in javascript. 您无法在javascript中以这种方式比较
Boolean
对象。 new Boolean(...) === false
will always evaluate to false
. new Boolean(...) === false
将始终评估为false
。
MDN has a good warning on the matter: MDN对此事提出了很好的警告 :
Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.
不要将原始布尔值true和false与布尔对象的true和false值混淆。
For instance (given value = arr[i]
), if you need to see if something is truthy just use !!value
. 例如(给定
value = arr[i]
),如果您需要查看某件事是否属实,请使用!!value
。 For falsey, use !value
. 对于false,请使用
!value
。 If you need to see if something is really true
use value === true
, likewise for false
, but not a Boolean
instance. 如果需要查看某项是否确实为
true
使用value === true
,同样对于false
,也不要使用Boolean
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.