arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

Number (JS Object)

hashtag
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 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 . The largest value a Number can hold is about 1.8E308. Values higher than that are replaced with the special Number constant .

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 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 .

hashtag

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 .

hashtag

Copy to Clipboard

hashtag

Copy to Clipboard

hashtag

Creates a new Number value.

hashtag

The smallest interval between two representable numbers.

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

The largest positive representable number.

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

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

Special "Not a Number" value.

Special value representing negative infinity. Returned on overflow.

Special value representing infinity. Returned on overflow.

Allows the addition of properties to the Number object.

hashtag

Determine whether the passed value is NaN.

Determine whether the passed value is a finite number.

Determine whether the passed value is an integer.

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

This is the same as the global function.

This is the same as the global function.

hashtag

Returns a string representing the number in exponential notation.

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

Returns a string with a language sensitive representation of this number. Overrides the method.

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

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

Returns the primitive value of the specified object. Overrides the method.

hashtag

hashtag

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

Copy to Clipboard

hashtag

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 .)

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 instead.

Larger numbers can be represented using the type.

hashtag

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

Copy to Clipboard

This logs 819199440000.

hashtag

double-precision 64-bit binary format IEEE 754arrow-up-right
roundingarrow-up-right
Infinityarrow-up-right
BigIntarrow-up-right
lexical grammar herearrow-up-right
Descriptionarrow-up-right
NaNarrow-up-right
Literal syntaxarrow-up-right
Function syntaxarrow-up-right
Constructorarrow-up-right
Number()arrow-up-right
Static propertiesarrow-up-right
Number.EPSILONarrow-up-right
Number.MAX_SAFE_INTEGERarrow-up-right
Number.MAX_VALUEarrow-up-right
Number.MIN_SAFE_INTEGERarrow-up-right
Number.MIN_VALUEarrow-up-right
Number.NaNarrow-up-right
Number.NEGATIVE_INFINITYarrow-up-right
Number.POSITIVE_INFINITYarrow-up-right
Number.prototypearrow-up-right
Static methodsarrow-up-right
Number.isNaN()arrow-up-right
Number.isFinite()arrow-up-right
Number.isInteger()arrow-up-right
Number.isSafeInteger()arrow-up-right
Number.parseFloat(string)arrow-up-right
parseFloat()arrow-up-right
Number.parseInt(string, [radix])arrow-up-right
parseInt()arrow-up-right
Instance methodsarrow-up-right
Number.prototype.toExponential(fractionDigits)arrow-up-right
Number.prototype.toFixed(digits)arrow-up-right
Number.prototype.toLocaleString([locales [, options]])arrow-up-right
Object.prototype.toLocaleString()arrow-up-right
Number.prototype.toPrecision(precision)arrow-up-right
Number.prototype.toString([radix])arrow-up-right
Object.prototype.toString()arrow-up-right
Number.prototype.valueOf()arrow-up-right
Object.prototype.valueOf()arrow-up-right
Examplesarrow-up-right
Using the Number object to assign values to numeric variablesarrow-up-right
Integer range for Numberarrow-up-right
6.1.6 The Number Typearrow-up-right
Stringarrow-up-right
BigIntarrow-up-right
Using Number to convert a Date objectarrow-up-right
Datearrow-up-right
Convert numeric strings and null to numbersarrow-up-right
123    // one-hundred twenty-three
123.0  // same
123 === 123.0  // true
Number('123')  // returns the number 123
Number('123') === 123  // true

Number("unicorn")  // NaN
Number(undefined)  // NaN
const biggestNum     = Number.MAX_VALUE
const smallestNum    = Number.MIN_VALUE
const infiniteNum    = Number.POSITIVE_INFINITY
const negInfiniteNum = Number.NEGATIVE_INFINITY
const notANum        = Number.NaN
const biggestInt  = Number.MAX_SAFE_INTEGER  //  (2**53 - 1) =>  9007199254740991
const smallestInt = Number.MIN_SAFE_INTEGER  // -(2**53 - 1) => -9007199254740991
let d = new Date('December 17, 1995 03:24:00')
console.log(Number(d))
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