JavaScript Number Object
JavaScript has only one number type.
You can write numbers 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 point
Very large or very small numbers can be written by scientific (exponential) notation:
Example
var y=123e5; // 12300000 var z=123e-5; // 0.00123
All JavaScript numbers are 64 bits
JavaScript is not a type 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 of floating-point type. JavaScript uses the 64-bit floating point format defined by the IEEE754 standard to represent numbers. It can represent the maximum value (Number.MAX_VALUE) as ±1.7976931348623157e+308 and the minimum value (Number.MIN_VALUE) as ±5e-324 .
This format uses 64 bits to store values, where 0 to 51 store numbers (slices), 52 to 62 store exponents, and 63 bits store signs:
Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Accuracy
Integers (without decimal point or exponential notation) can be up to 15 digits.
Example
var x = 999999999999999; // x is 999999999999999 var y = 9999999999999999; // y is 10000000000000000
The maximum number of decimal places is 17, but floating-point operations are not always 100% accurate:
Example
var x = 0.2+0.1; // The output result is 0.30000000000000004
Octal and Hexadecimal
If the prefix is 0, JavaScript will interpret the numeric constant as an octal number, and if the prefix is 0 and "x", it will be interpreted as a hexadecimal number.
Example
var y = 0377; var z = 0xFF;
![]() |
Never write a zero in front of a number unless you need to perform an octal conversion. |
---|
By default, JavaScript numbers are displayed in decimal.
But you can use the toString() method to output hexadecimal, octal, and binary.
Example
var myNumber=128; myNumber.toString(16); // returns 80 myNumber.toString(8); // returns 200 myNumber.toString(2); // returns 10000000
Infinity
When the result of a number operation 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. The behavioral characteristics of infinite values are consistent with what we expect: the results of addition, subtraction, multiplication, and division based on them are still infinite (of course, their signs are retained).
Example
myNumber=2; while (myNumber!=Infinity) { myNumber=myNumber*myNumber; // Repeat the calculation until myNumber is equal to Infinity }
Dividing by 0 also produces infinity:
Example
var x = 2/0; var y = -2/0;
NaN - Not a number 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. You can set the Number object 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.
Example
var x = 1000 / "Apple"; isNaN(x); // returns true var y = 100 / "1000"; isNaN(y); // returns false
Divide by 0 is infinity, and infinity is a number:
Example
var x = 1000 / 0; isNaN(x); // returns false
Numbers can be numbers or objects
Numbers can be initialized with private data, like x = 123;
JavaScript numeric object initialization data, var y = new Number(123);
Example
var x = 123; var y = new Number(123); typeof(x) // return Number typeof(y) // return Object
Example
var x = 123; var y = new Number(123); (x === y) // is false, because x is a number and y is an object
Number property
Attributes | Description |
---|---|
Number.MAX_VALUE | Max |
Number.MIN_VALUE | Minimum |
Number.NaN | Not a number |
Number.NEGATIVE_INFINITY | Negative infinity, return on overflow |
Number.POSITIVE_INFINITY | Positive infinity, return on overflow |
Number.EPSILON |
Represents the difference between 1 and the smallest number that is closest to 1 and greater than 1 |
Number.MIN_SAFE_INTEGER | The smallest safe integer. |
Number.MAX_SAFE_INTEGER | The largest safe integer. |
Digital method
Method | Description |
---|---|
Number.parseFloat() | Converting a string to a floating point number is consistent with the global method parseFloat() . |
Number.parseInt() |
Converting a string to an integer number is consistent with the global method parseInt() . |
Number.isFinite() | Determine whether the passed parameter is a finite number. |
Number.isInteger() | Determine whether the passed parameter is an integer. |
Number.isNaN() | Determine whether the passed parameter is isNaN(). |
Number.isSafeInteger() | Determine whether the passed parameter is a safe integer. |
Some methods on digital type prototype
Method | Description |
---|---|
toExponential() | Returns a string in exponential form of a number, such as: 1.23e+2 |
toFixed() |
Returns the representation of the specified number of decimal places. var a=123;b=a.toFixed(2); // b="123.00" |
toPrecision() |
Returns a number with the specified precision. In the following example, in a=123, 3 will be disappeared due to precision limitations: var a=123;b=a.toPrecision(2); // b="1.2e+2" |