为什么在 JavaScript 中将值与未定义的 returns 进行比较是错误的?

Why does comparing value with undefined returns false in JavaScript?

我是 Javascript 的新手,我注意到当一个变量是 undefined 时,比较数字 returns false 如下。为什么将 undefined 与数字 return false 进行比较?

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

这就是 JavaScript 中的工作方式。

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

所以,当你比较它时,强制检查如下:

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

因此,

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