false 同时 console.log(Infinity==In-6ren">
gpt4 book ai didi

javascript - 为什么 JavaScript 中的 "Infinity==Infinity"变为真?

转载 作者:行者123 更新时间:2023-11-29 17:53:47 30 4
gpt4 key购买 nike

据我所知,在数学中 InfinityNaN 都是模糊值。众所周知:

console.log(NaN == NaN); //-> false

同时

console.log(Infinity==Infinity); //-> true

我想知道为什么第二个代码的结果是true。我希望第二个的结果应该是 false,但事实并非如此。

你能帮帮我吗?我真的很感激。谢谢。

最佳答案

这就是为什么:

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN

The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number. This value behaves mathematically like infinity; for example, any positive number multiplied by Infinity is Infinity, and anything divided by Infinity is 0.

来源:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity

您可能还对使用 NumberisFinite 方法感兴趣:

Number.isFinite(Infinity);  // false
Number.isFinite(NaN); // false

阅读 Number.isFinite():https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite

关于javascript - 为什么 JavaScript 中的 "Infinity==Infinity"变为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41025686/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com