isFinite(Number.MAX_VALUE + 1)返回true?

时间:2014-08-27 16:19:57

标签: javascript numbers max

我正在学习JavaScript,书中有一个例子:

<!DOCTYPE html>
<html>
<head>
    <title>Number Example 2</title>
    <script type="text/javascript">

        var result = Number.MAX_VALUE + 1;
        alert(isFinite(result));    // the book says it should be false, but I got "true"

    </script>

</head>
<body>

</body>
</html>

我收到警告消息&#34; true&#34; 运行此代码,有什么问题吗?

1 个答案:

答案 0 :(得分:1)

Number.MAX_VALUE加上任何其他值返回Number.MAX_VALUE

因此,例如,以下语句为true

 Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 2

因为,我们以及以下陈述也是true

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 999999

在某些时候,语句变为false但是:

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 // true

但又添加一个9会返回false

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 // false

然而,如果我们将Number.MAX_VALUE添加到自身,它将返回以下内容:

Number.MAX_VALUE + (Number.MAX_VALUE) === Infinity

使用乘法有点棘手。当我们将Number.MAX_VALUE乘以高于1的任何值时,它会返回Infinity

Number.MAX_VALUE * 1.00000001 === Infinity