Number

Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25.

The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.

Description

Numbers are most commonly expressed in literal forms like 0b101, 0o13, 0x0A. The lexical grammar contains a more detailed reference.

123; // one-hundred twenty-three
123.0; // same
123 === 123.0; // true

A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript also has a BigInt type, but it's not designed to replace Number for everyday uses. 37 is still a number, not a BigInt.)

When used as a function, Number(value) converts a string or other value to the Number type. If the value can't be converted, it returns NaN.

Number("123"); // returns the number 123
Number("123") === 123; // true

Number("unicorn"); // NaN
Number(undefined); // NaN

Number encoding

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. This means it can represent fractional values, but there are some limits to the stored number's magnitude and precision. Very briefly, a IEEE 754 double-precision number uses 64 bits to represent 3 parts:

  • 1 bit for the sign (positive or negative)
  • 11 bits for the exponent (-1022 to 1023)
  • 52 bits for the mantissa (representing a number between 0 and 1)

The mantissa (also called significand) is the part of the number representing the actual value (significant digits). The exponent is the power of 2 that the mantissa should be multiplied by. Thinking about it as scientific notation:

Number = ( 1 ) sign ( 1 + mantissa ) 2 exponent \text{Number} = ({-1})^{\text{sign}} \cdot (1 + \text{mantissa}) \cdot 2^{\text{exponent}}

The mantissa is stored with 52 bits, interpreted as digits after 1.… in a binary fractional number. Therefore, the mantissa's precision is 2-52 (obtainable via Number.EPSILON), or about 15 to 17 decimal places; arithmetic above that level of precision is subject to rounding.

The largest value a number can hold is 21024 - 1 (with the exponent being 1023 and the mantissa being 0.1111… in base 2), which is obtainable via Number.MAX_VALUE. Values higher than that are replaced with the special number constant Infinity.

Integers can only be represented without loss of precision in the range -253 + 1 to 253 - 1, inclusive (obtainable via Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER), because the mantissa can only hold 53 bits (including the leading 1).

More details on this are described in the ECMAScript standard.

Constructor

Number()

Creates a new Number value.

When Number is called as a constructor (with new), it creates a Number object, which is not a primitive. For example, typeof new Number(42) === "object", and new Number(42) !== 42 (although new Number(42) == 42).

Warning: You should rarely find yourself using Number as a constructor.

Static properties

Number.EPSILON

The smallest interval between two representable numbers.

Number.MAX_SAFE_INTEGER

The maximum safe integer in JavaScript (253 - 1).

Number.MAX_VALUE

The largest positive representable number.

Number.MIN_SAFE_INTEGER

The minimum safe integer in JavaScript (-(253 - 1)).

Number.MIN_VALUE

The smallest positive representable number—that is, the positive number closest to zero (without actually being zero).

Number.NaN

Special "Not a Number" value.

Number.NEGATIVE_INFINITY

Special value representing negative infinity. Returned on overflow.

Number.POSITIVE_INFINITY

Special value representing infinity. Returned on overflow.

Number.prototype

Allows the addition of properties to the Number object.

Static methods

Number.isNaN()

Determine whether the passed value is NaN.

Number.isFinite()

Determine whether the passed value is a finite number.

Number.isInteger()

Determine whether the passed value is an integer.

Number.isSafeInteger()

Determine whether the passed value is a safe integer (number between -(253 - 1) and 253 - 1).

Number.parseFloat()

This is the same as the global parseFloat() function.

Number.parseInt()

This is the same as the global parseInt() function.

Instance methods

Number.prototype.toExponential()

Returns a string representing the number in exponential notation.

Number.prototype.toFixed()

Returns a string representing the number in fixed-point notation.

Number.prototype.toLocaleString()

Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.

Number.prototype.toPrecision()

Returns a string representing the number to a specified precision in fixed-point or exponential notation.

Number.prototype.toString()

Returns a string representing the specified object in the specified radix ("base"). Overrides the Object.prototype.toString() method.

Number.prototype.valueOf()

Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.

Examples

Using the Number object to assign values to numeric variables

The following example uses the Number object's properties to assign values to several numeric variables:

const biggestNum = Number.MAX_VALUE;
const smallestNum = Number.MIN_VALUE;
const infiniteNum = Number.POSITIVE_INFINITY;
const negInfiniteNum = Number.NEGATIVE_INFINITY;
const notANum = Number.NaN;

Integer range for Number

The following example shows the minimum and maximum integer values that can be represented as Number object.

const biggestInt = Number.MAX_SAFE_INTEGER; //  (2**53 - 1) =>  9007199254740991
const smallestInt = Number.MIN_SAFE_INTEGER; // -(2**53 - 1) => -9007199254740991

When parsing data that has been serialized to JSON, integer values falling outside of this range can be expected to become corrupted when JSON parser coerces them to Number type.

A possible workaround is to use String instead.

Larger numbers can be represented using the BigInt type.

Using Number() to convert a Date object

The following example converts the Date object to a numerical value using Number as a function:

const d = new Date("December 17, 1995 03:24:00");
console.log(Number(d));

This logs 819199440000.

Convert numeric strings and null to numbers

Number("123"); // 123
Number("123") === 123; // true
Number("12.3"); // 12.3
Number("12.00"); // 12
Number("123e-1"); // 12.3
Number(""); // 0
Number(null); // 0
Number("0x11"); // 17
Number("0b11"); // 3
Number("0o11"); // 9
Number("foo"); // NaN
Number("100a"); // NaN
Number("-Infinity"); // -Infinity

Specifications

Specification
ECMAScript Language Specification
# sec-number-objects

Browser compatibility

BCD tables only load in the browser

See also