Number

Questa traduzione è incompleta. Aiutaci a tradurre questo articolo dall’inglese

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.

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 what it can store. A Number only keeps about 17 decimal places of precision; arithmetic is subject to rounding. The largest value a Number can hold is about 1.8×10308. Numbers beyond that are replaced with the special Number constant Infinity.

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

Description

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.

Literal syntax

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

Function syntax

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

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

Constructor

Number()
Creates a new Number value.

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(string)
This is the same as the global parseFloat() function.
Number.parseInt(string, [radix])
This is the same as the global parseInt() function.

Instance methods

Number.prototype.toExponential(fractionDigits)
Returns a string representing the number in exponential notation.
Number.prototype.toFixed(digits)
Returns a string representing the number in fixed-point notation.
Number.prototype.toLocaleString([locales [, options]])
Returns a string with a language sensitive representation of this number. Overrides the Object.prototype.toLocaleString() method.
Number.prototype.toPrecision(precision)
Returns a string representing the number to a specified precision in fixed-point or exponential notation.
Number.prototype.toString([radix])
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. (More details on this are described in the ECMAScript standard, chapter 6.1.6 The Number Type.)

const biggestInt  = Number.MAX_SAFE_INTEGER  //  (253 - 1) =>  9007199254740991
const smallestInt = Number.MIN_SAFE_INTEGER  // -(253 - 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:

let 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 (ECMA-262)
The definition of 'Number' in that specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
NumberChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
EPSILONChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
MAX_SAFE_INTEGERChrome Full support 34Edge Full support 12Firefox Full support 31IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 31Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
MAX_VALUEChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
MIN_SAFE_INTEGERChrome Full support 34Edge Full support 12Firefox Full support 31IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 31Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
MIN_VALUEChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
NaNChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
NEGATIVE_INFINITYChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
Number() constructorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
POSITIVE_INFINITYChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
isFiniteChrome Full support 19Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 15Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 25Firefox Android Full support 16Opera Android Full support 14Safari iOS Full support 9Samsung Internet Android Full support 1.5nodejs Full support 0.10
isIntegerChrome Full support 34Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 16Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
isNaNChrome Full support 25Edge Full support 12Firefox Full support 15IE No support NoOpera Full support 15Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 25Firefox Android Full support 15Opera Android Full support 14Safari iOS Full support 9Samsung Internet Android Full support 1.5nodejs Full support 0.10
isSafeIntegerChrome Full support 34Edge Full support 12Firefox Full support 32IE No support NoOpera Full support 21Safari Full support 10WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 32Opera Android Full support 21Safari iOS Full support 10Samsung Internet Android Full support 2.0nodejs Full support 0.12
parseFloatChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
parseIntChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
toExponentialChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 7Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
toFixedChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 7Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
toLocaleStringChrome Full support 1Edge Full support 12
Notes
Full support 12
Notes
Notes Before Edge 18, numbers are rounded to 15 decimal digits. For example, (1000000000000005).toLocaleString('en-US') returns "1,000,000,000,000,010".
Firefox Full support 1IE Full support 5
Notes
Full support 5
Notes
Notes In Internet Explorer 11, numbers are rounded to 15 decimal digits. For example, (1000000000000005).toLocaleString('en-US') returns "1,000,000,000,000,010".
Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
toPrecisionChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 7Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 74
Notes
No support 1 — 74
Notes
Notes Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
valueOfChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
See implementation notes.
See implementation notes.

See also