[英]In if statement,undefined equals with false
I'm confused with the code below:我对下面的代码感到困惑:
if(undefined){
//code will not be executed
}
and和
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?这是否意味着“未定义”等于假?
Here the question related,but no one point above situation out.这里的问题相关,但没有一点以上情况。
It means that undefined
is a falsy value, list of falsy values are:这意味着undefined
是一个假值,假值列表是:
"" // Empty string
null // null
undefined // undefined, which you get when doing: var a;
false // Boolean false
0 // Number 0
NaN // Not A Number eg: "a" * 2
If you negate a falsy value you will get true:如果你否定一个假值,你会得到真:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truthy value you will get false:当你 nagate 一个真值时,你会得到假:
!"hello" === false
!1 === false
But undefined
is not equal false
:但是undefined
不等于false
:
undefined === false // false
undefined == false // false
And just for the fun if it:只是为了好玩,如果它:
undefined == null // true
In javascript strict mode, undefined
is not false, but javascript try to convert the object or var to a boolean
value (this is called in javascript truthy value ), that's the reason you got an undefined
as false.在 javascript 严格模式下, undefined
不是假的,但 javascript 尝试将对象或 var 转换为boolean
值(这在 javascript真值中称为),这就是您将undefined
设为假的原因。 This happens with null also, for example.例如,这也发生在 null 上。
You can force that with this strict no equality:你可以用这个严格的不相等来强制它:
if(undefined!==false) console.log("Is not false");
Please take a look below checked falsy values:请看下面检查的虚假值:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false
, undefined == false
, null == NaN
, and NaN == false
are not true
That means they are not equal.我们可以看到null == false
、 undefined == false
、 null == NaN
和NaN == false
不是true
这意味着它们不相等。 From the above result, we got 3 falsy values group:从上面的结果中,我们得到了 3 个假值组:
But a negative falsy value is always true:但是一个负的假值总是真的:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example: To check true
value of dataTitle
variable例如:检查dataTitle
变量的true
值
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group上面的语句将检查 false 组以及 null 组
To check false
value of dataTitle
variable检查dataTitle
变量的false
值
if(!dataTitle)
{
console.log('hi');
}
//or
if(dataTitle==null || dataTitle===false)
console.log('hi');
In javascript, undefined
and null
are empty properties declared on the global scope. Their value doesn't equal false
;在 javascript 中, undefined
和null
是在全局 scope 上声明的空属性。它们的值不等于false
; they both have the initial value of primitive undefined .它们都具有原始值undefined的初始值。
undefined == false // false
With that said, both will result in false
value upon a programmatic true/false evaluation.话虽如此,两者都会在程序化的真/假评估中产生false
的价值。 In your example, you used a logical NOT operator (MDN):在您的示例中,您使用了逻辑 NOT运算符 (MDN):
Returns false if its single operand can be converted to true;如果其单个操作数可以转换为 true,则返回 false; otherwise, returns true否则,返回真
The negation operator first evaluated undefined
as false
, and then negated the value to true
.否定运算符首先将undefined
计算为false
,然后将该值取反为true
。 You can illustrate the process roughly like this:你可以大致这样说明这个过程:
if (!(!!undefined)) {
// code will be executed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.