JS Number Object
Number object
The Number object is a wrapper object for the original numeric value.
Number is created as new Number().
Grammar
var num = new Number(value);
Note: If a parameter value cannot be converted to a number, NaN (non-numeric value) is returned.
Number object tutorial, please refer to JavaScript Number object tutorial .
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, which is returned when overflowing. |
NaN | Non-numeric values. |
POSITIVE_INFINITY | Positive infinity, returns the value when overflowing. |
prototype | Allows you to add properties and methods to objects. |
Number object methods
Method | Description |
---|---|
isFinite | Detects whether the specified parameter is infinity. |
toExponential(x) | Converts the value of the object to exponential notation. |
toFixed(x) | Converts a number to a string with the result of a number of digits specified after the decimal point. |
toPrecision(x) | Formats numbers to the specified length. |
toString() | Converts a number to a string, using the specified cardinality. |
valueOf() | Returns the base numeric value of a Number object. |
ES6 Added Number property
ES 6 adds the following three properties for the Number object:
EPSILON: Represents the difference between 1 and the smallest Number that is closer to 1 and greater than 1
MIN_SAFE_INTEGER: Represents the smallest secure integer type 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;
ES6 adds a Number method
ES 6 adds the following two methods for the Number object:
Number.isInteger(): Used to determine whether a given argument is an integer.
Number.isSafeInteger(): Determines whether the value of the parameter passed in is a "safe integer".
Number.isInteger() returns true when the argument is an integer.
Example
Number.isInteger(10); // return true Number.isInteger(10.5); // return false
Number.isSafeInteger() determines whether the value of the parameter passed in is a "safe integer".
The safe integer range is an integer between , containing -(253 - 1) till
253 - 1, includes
-(253 - 1) and
253 - 1 .
Example
Number.isSafeInteger(10); // return true Number.isSafeInteger(12345678901234567890); // return false