JavaScript has only one number type.
Numbers can be written with or without a decimal point.
JavaScript Numbers
JavaScript numbers can be written with or without a decimal point:
Example
var
pi=3.14; // Use decimal point
var
x=34; // Do not use decimal points
Very large or very small numbers can be written using scientific (exponential) notation:
Example
var y=123e5; // 12300000
var z=123e-5; // 0.00123
##All JavaScript numbers are 64 bits
JavaScript is not a typed language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integer, short, long, floating point, etc.
In JavaScript, numbers are not divided into integer types and floating point types. All numbers are represented by
Floating point type. JavaScript uses the 64-bit floating point format defined by the IEEE754 standard to represent numbers. It can represent a maximum value of ±1.7976931348623157 x 10308 and a minimum value of ±5 x 10 -324
(aka Fraction/Mantissa) | INDEX | Sign |
52 bits (0 - 51) | 11 bits (50 - 62) | 1 bit (63) |
Precision
Integer (does not use decimal point or exponent notation) ) is up to 15 digits.
The maximum number of decimal places is 17, but floating point operations are not always 100% accurate:
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var x;
document.write("<p>仅显示17位: ");
x=12345678901234567890;
document.write(x + "</p>");
document.write("<p>0.2 + 0.1 = ");
x=0.2+0.1;
document.write(x + "</p>");
document.write("<p>可以通过乘以10或除以10来计算: ");
x=(0.2*10+0.1*10)/10;
document.write(x +"</p>");
</script>
</body>
</html>
Run instance»
Click the "Run instance" button to view the online instance
Octal and hexadecimal
If the prefix is 0, then JavaScript interprets numeric constants as octal numbers, or as hexadecimal numbers if prefixed with 0 and "x".
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var y = 0377;
var z = 0xFF;
document.write(y + "<br>");
document.write(z + "<br>");
</script>
</body>
</html>
Run instance»
Click the "Run instance" button to view the online instance
| Never write zeros in front of numbers unless you need to do an octal conversion. |
---|
By default, JavaScript numbers are displayed in decimal.
But you can use the toString() method to output hexadecimal, octal, or binary.
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var myNumber = 128;
document.write(myNumber + ' 十进制<br>');
document.write(myNumber.toString(16) + ' 十六进制<br>');
document.write(myNumber.toString(8) + ' 八进制<br>');
document.write(myNumber.toString(2) + ' 二进制<br>');
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Infinity
When the numerical operation result exceeds the upper limit of the number that JavaScript can represent (overflow), the result is a special infinity value, which is represented by Infinity in JavaScript . Similarly, when the value of a negative number exceeds the range of negative numbers that JavaScript can represent, the result is negative infinity, which is represented by -Infinity in JavaScript. Infinite values behave as we would expect: operations based on their addition, subtraction, multiplication, and division still result in infinity (while retaining their signs, of course).
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
myNumber=2;
while (myNumber!=Infinity){
myNumber=myNumber*myNumber;
document.write(myNumber +'<BR>');
}
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Dividing by 0 also produces infinity:
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var x = 2/0;
var y = -2/0;
document.write(x + "<br>");
document.write(y + "<br>");
</script>
</body>
</html>
Run Instance»Click "Run Instance" " button to view online examples
NaN - Non-numeric value
The NaN attribute is a special value that represents a non-numeric value. This attribute is used to indicate that a value is not a number. A Number object can be set to this value to indicate that it is not a numeric value.
You can use the isNaN() global function to determine whether a value is a NaN value.
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p>一个数字除以一个字符串结果不是一个数字</p>
<p>一个数字除以一个字符串数字结果是一个数字</p>
<p id="demo"></p>
<script>
var x = 1000 / "Apple";
var y = 1000 / "1000";
document.getElementById("demo").innerHTML = isNaN(x) + "<br>" + isNaN(y);
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Divided by 0 is infinity, infinity is a number:
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo"></p>
<script>
var y = 1000 / 0;
document.getElementById("demo").innerHTML = isNaN(y);
</script>
</body>
</html>
Run Example»Click "Run instance" button to view the online instance
The number can be a number or an object
The number can be initialized with private data, like
x = 123;
JavaScript digital object initialization data, var y = new
Number(123);
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo"></p>
<script>
var x = 123; // x 是一个数字
var y = new Number(123); // y 是一个对象
var txt = typeof(x) + " " + typeof(y);
document.getElementById("demo").innerHTML=txt;
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Instance
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo"></p>
<script>
var x = 123; // x 是一个数字
var y = new Number(123); // y 是一个对象
document.getElementById("demo").innerHTML = x===y;
</script>
</body>
</html>
Run Instance»Click the "Run Instance" button to view the online instance
Number attributes
MAX_VALUE
- ##MIN_VALUE
- NEGATIVE_INFINITY
- POSITIVE_INFINITY
- NaN
- prototype
- constructor
Number methods- toExponential()
- toFixed()
- toPrecision()
- toString()
- valueOf()