JavaScript Number object
Number object
The Number object is a wrapper object for primitive numeric values.
Number is created with new Number().
Syntax
var num = new Number(value);
Note: If an argument value cannot be converted to a number, NaN (Not a Number) is returned.
Number object properties
| property | Description |
|---|---|
| constructor | Returns a reference to the Number function that created this object. |
| MAX_VALUE | The largest number that can be represented. |
| MIN_VALUE | The smallest number that can be represented. |
| NEGATIVE_INFINITY | Negative infinity, returned on overflow. |
| NaN | A non-numeric value. |
| POSITIVE_INFINITY | Positive infinity, returned on overflow. |
| prototype | Allows you to add properties and methods to an object. |
Number object methods
| Method | Description |
|---|---|
| isFinite | Checks whether the specified argument is infinity. |
| isInteger | Checks whether the specified argument is an integer. |
| isNaN | Checks whether the specified argument is NaN. |
| isSafeInteger | Checks whether the specified argument is a safe integer. |
| toExponential(x) | Converts the value of an object to exponential notation. |
| toFixed(x) | Converts a number to a string with a specified number of digits after the decimal point. |
| toLocaleString(locales, options) | Returns a string representing the number in a specific language environment. |
| toPrecision(x) | Formats a number to a specified length. |
| toString() | Converts a number to a string using a specified radix. |
| valueOf() | Returns the primitive numeric value of a Number object. |
New Number properties in ES6
ES 6 adds the following three Number object properties:
- EPSILON: represents the difference between 1 and the smallest Number greater than 1.
- MIN_SAFE_INTEGER: represents the smallest safe integer number in JavaScript (
-(253 - 1)). - MAX_SAFE_INTEGER: represents the largest safe integer in JavaScript (
253 - 1).
Example
var
x
=
Number
.
EPSILON
;
var
y
=
Number
.
MIN_SAFE_INTEGER
;
var
z
=
Number
.
MAX_SAFE_INTEGER
;
Try it »
New Number methods in ES6
ES 6 adds the following two Number object methods:
- Number.isInteger(): used to determine whether the given argument is an integer.
- Number.isSafeInteger(): determines whether the passed argument value is a "safe integer".
Number.isInteger() returns true when the argument is an integer.
Example
Number
.
isInteger
(
10
)
;
//
Returns true
Number
.
isInteger
(
10.5
)
;
//
Returns false
Try it »
Number.isSafeInteger() determines whether the passed argument value is a "safe integer".
The range of safe integers is -(253 - 1)到 253 - 1integers between, including -(253 - 1)和 253 - 1.
Example
Number
.
isSafeInteger
(
10
)
;
//
Returns true
Number
.
isSafeInteger
(
12345678901234567890
)
;
//
Returns false
Try it »