JavaScript NumberObject


Number Object

The Number object is a wrapper object for primitive numeric values.

Number creation method: new Number().

Syntax

var num = new Number(value);

Note:If a parameter 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 representable number.
MIN_VALUE The smallest representable number.
NEGATIVE_INFINITY Negative infinity; returned on overflow.
NaN Not-a-number 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 Detects whether the specified parameter is infinity.
isInteger Detects whether the specified parameter is an integer.
isNaN Detects whether the specified parameter is NaN.
isSafeInteger Detects whether the specified parameter 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 representation of the number in a specific locale.
toPrecision(x) Formats a number to a specified length.
toString() Converts a number to a string, using a specified base (radix).
valueOf() Returns the primitive numeric value of a Number object.

ES6 New Number Properties

ES6 added the following three Number object properties:

  • EPSILON: Represents the difference between 1 and the smallest Number greater than 1 that is closest to 1.
  • MIN_SAFE_INTEGER: Represents the smallest safe integer in JavaScript (-(253 - 1))。
  • MAX_SAFE_INTEGER: Represents the largest safe integer in JavaScript (253 - 1)。

Examples

var x = Number.EPSILON; var y = Number.MIN_SAFE_INTEGER; var z = Number.MAX_SAFE_INTEGER;

Try it yourself »

ES6 New Number Methods

ES6 added the following two Number object methods:

  • Number.isInteger(): Used to determine whether the given parameter is an integer.
  • Number.isSafeInteger(): Determines whether the passed parameter value is a "safe integer".

Number.isInteger() returns true if the parameter is an integer.

Examples

Number.isInteger(10); //Returns true Number.isInteger(10.5); //Returns false

Try it yourself »

Number.isSafeInteger() determines whether the passed parameter value is a "safe integer".

The safe integer range is-(253 - 1)到 253 - 1 integers between, inclusive.-(253 - 1)和 253 - 1。

Examples

Number.isSafeInteger(10); //Returns true Number.isSafeInteger(12345678901234567890); //Returns false

Try it yourself »
Other Extensions