githubEdit

🔢Number (JS Object)

Number

Number is a primitive wrapper objectarrow-up-right 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.

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754arrow-up-right value, like double in Java or C#. This means it can represent fractional values, but there are some limits to what it can store. A Number only keeps about 17 decimal places of precision; arithmetic is subject to roundingarrow-up-right. The largest value a Number can hold is about 1.8E308. Values higher than that are replaced with the special Number constant Infinityarrow-up-right.

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 now has a BigIntarrow-up-right type, but it was not designed to replace Number for everyday uses. 37 is still a Number, not a BigInt.)

Number may also be expressed in literal forms like 0b101, 0o13, 0x0A. Learn more on numeric lexical grammar herearrow-up-right.

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 NaNarrow-up-right.

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

Copy to Clipboard

Number('123')  // returns the number 123
Number('123') === 123  // true

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

Copy to Clipboard

Number()arrow-up-right

Creates a new Number value.

Number.EPSILONarrow-up-right

The smallest interval between two representable numbers.

Number.MAX_SAFE_INTEGERarrow-up-right

The maximum safe integer in JavaScript (2^53 - 1).

Number.MAX_VALUEarrow-up-right

The largest positive representable number.

Number.MIN_SAFE_INTEGERarrow-up-right

The minimum safe integer in JavaScript (-(2^53 - 1)).

Number.MIN_VALUEarrow-up-right

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

Number.NaNarrow-up-right

Special "Not a Number" value.

Number.NEGATIVE_INFINITYarrow-up-right

Special value representing negative infinity. Returned on overflow.

Number.POSITIVE_INFINITYarrow-up-right

Special value representing infinity. Returned on overflow.

Number.prototypearrow-up-right

Allows the addition of properties to the Number object.

Number.isNaN()arrow-up-right

Determine whether the passed value is NaN.

Number.isFinite()arrow-up-right

Determine whether the passed value is a finite number.

Number.isInteger()arrow-up-right

Determine whether the passed value is an integer.

Number.isSafeInteger()arrow-up-right

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

Number.parseFloat(string)arrow-up-right

This is the same as the global parseFloat()arrow-up-right function.

Number.parseInt(string, [radix])arrow-up-right

This is the same as the global parseInt()arrow-up-right function.

Number.prototype.toExponential(fractionDigits)arrow-up-right

Returns a string representing the number in exponential notation.

Number.prototype.toFixed(digits)arrow-up-right

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

Number.prototype.toLocaleString([locales [, options]])arrow-up-right

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

Number.prototype.toPrecision(precision)arrow-up-right

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

Number.prototype.toString([radix])arrow-up-right

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

Number.prototype.valueOf()arrow-up-right

Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf()arrow-up-right method.

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

Copy to Clipboard

The following example shows the minimum and maximum integer values that can be represented as Number object. (More details on this are described in the ECMAScript standard, chapter 6.1.6 The Number Typearrow-up-right.)

Copy to Clipboard

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 Stringarrow-up-right instead.

Larger numbers can be represented using the BigIntarrow-up-right type.

The following example converts the Datearrow-up-right object to a numerical value using Number as a function:

Copy to Clipboard

This logs 819199440000.

Last updated