由于内存限制,ECMAScript数值具有范围,最小数值Nunber.MIN_VALUE 为5e-324,最大数值Nunber.MAX_VALUE为1.7976931348623157e+308。当数值超出这个范围后,就会不正常显示,具体情况如下:
-infinity | 正常显示 | -0 | 0 | 正常显示 | infinity |
num<=1.7976931348623157e+308 | -1.7976931348623157e+308<num<-5e-324 | -5e-324<num<0 | 0<=num<=5e-423 | 5e-423<num<1.7976931348623157e+308 | num>=1.7976931348623157e+308 |
即:-infinity--0--infinity
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数值范围P29</title>
</head>
<body>
<script>
var num1=5e-400,
num2=2e308,
num3=-5e-400,
num4=-2e308;
console.log(num1);
console.log(num2);
console.log(num3);
console.log(num4);
console.log(Number.MAX_VALUE);
console.log(Number.MIN_VALUE);
</script>
</body>
</html>
得到的结果:
如果想检验一个数是否在范围内,可以使用isFinite()函数:
console.log(isFinite(num1));
console.log(isFinite(num2));
得到的结果:
![大笑](https://static-blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)