JavaScript Number Reference Manual
- Previous Page JS Math
- Next Page JS Promise
JavaScript Numbers
JavaScript has only one type of number.
Numbers can be written with decimals or without decimals:
Example 1
let x = 3.14; // A number with decimals let y = 34; // A number without decimals
Very large or very small numbers can be written in scientific (exponential) notation:
Example 2
let x = 123e5; // 12300000 let y = 123e-5; // 0.00123
For more information about JavaScript numbers, please read our JavaScript Number Tutorial.
JavaScript Number methods and properties
Name | Description |
---|---|
constructor | Returns a reference to the Number function that created this object. |
EPSILON | Returns a reference to the Number function that created this object. |
isFinite() | Check if the value is a finite number. |
isInteger() | Checks if the value is an integer. |
isNaN() | Check if the value is Number.NaN. |
isSafeInteger() | Checks if the value is a safe integer. |
MAX_SAFE_INTEGER | Returns a reference to the Number function that created this object. |
MIN_SAFE_INTEGER | Returns a reference to the Number function that created this object. |
MAX_VALUE | Largest number that can be represented. |
MIN_VALUE | Smallest number that can be represented. |
NaN | Non-numeric value. |
NEGATIVE_INFINITY | Negative infinity, returned when overflow occurs. |
POSITIVE_INFINITY | Positive infinity, returned when overflow occurs. |
parseFloat() | Checks if the value is an integer. |
parseInt() | Checks if the value is an integer. |
prototype | Allows you to add properties and methods to the object. |
toExponential(x) | Converts a number to exponential notation. |
toFixed(x) | Converts a number to a string, with a specified number of digits after the decimal point. |
toLocaleString() | Converts a number to a string using the local numeric format order. |
toPrecision(x) | Formats a number to a specified length. |
toString() | Converts a number to a string. |
valueOf() | Returns the original value of the number (basic numeric value). |
Tip:All numeric methods return a new value. They do not change the original variable.
Description of Number object
In JavaScript, numbers are a basic data type. JavaScript also supports the Number object, which is a wrapper object for primitive numbers. JavaScript will automatically convert between primitive data and objects as needed. In JavaScript 1.1, the constructor Number() can be used explicitly to create a Number object, although there is no need to do so.
The constructor Number() can be used without the new operator, and can be used directly as a conversion function. When Number() is called in this way, it converts its parameter into a number and then returns the original value after conversion (or NaN).
The constructor is also often used as a placeholder for 5 useful numeric constants, which arelargest number that can be represented、smallest number that can be represented、positive infinity、negative infinityandSpecial NaN valueNote that these values are properties of the Number() constructor itself, not properties of a specific Number object.
For example, using the property MAX_VALUE is correct:
var big = Number.MAX_VALUE
But this is wrong:
var n= new Number(2); var big = n.MAX_VALUE
For comparison, let's look at the toString() method and other methods of the Number object, which are methods of each Number object, not methods of the Number() constructor. As mentioned before, JavaScript will automatically convert primitive numeric values to Number objects when necessary, and the methods of Number can be called on either Number objects or primitive numeric values.
var n = 123; var binary_value = n.toString(2);
Supplementary Reading
For more information, please read the relevant content in the advanced JavaScript tutorial:
- ECMAScript Reference Type
- Reference types are often called classes or objects. This section explains the predefined reference types of ECMAScript.
- Previous Page JS Math
- Next Page JS Promise