简体   繁体   English

为什么在JavaScript中将value与undefined的比较返回false?

[英]Why does comparing value with undefined returns false in JavaScript?

I am new to Javascript and I noticed when a variable is undefined , comparing a number returns false as below. 我是Javascript的新手,我注意到当一个变量undefined ,比较一个数字会返回false ,如下所示。 Why does comparing undefined with numbers return false ? 为什么将undefined与数字进行比较会返回false

 var a = undefined; console.log(a < 10); console.log(10 < a); console.log(a == 10); 

This is how works in JavaScript. 这是在JavaScript中的工作原理。

Number(undefined) // NaN
NaN == NaN // false
NaN < 0 // false
NaN > 0 // false

So, while you compare it forces to check like: 所以,当你比较力量来检查时:

Number(undefined) < 10
// undefined is coerced to check with number

And thus, 因此,

undefined == 10 // false
undefined > 10 // false
undefined < 10 // false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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