Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Number object

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:

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() 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 »