Önkénteseink még nem fordították le ezt a cikket magyar nyelvre. Csatlakozzon, és segítsen a munka befejezésében!
Elolvashatja a cikket English (US) nyelven is.

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor.

Syntax

new Number(value);

Parameters

value
The numeric value of the object being created.

Description

The primary uses of the Number object are:

  • If the argument cannot be converted into a number, it returns NaN.
  • In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.

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 a Number object.

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.toInteger()
Used to evaluate the passed value and convert it to an integer (or Infinity), but has been removed.
Number.parseFloat()
The value is the same as parseFloat() of the global object.
Number.parseInt()
The value is the same as parseInt() of the global object.

Number instances

All Number instances inherit from Number.prototype. The prototype object of the Number constructor can be modified to affect all Number instances.

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.toSource()
Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.prototype.toSource() method.
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:

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

Integer range for Number

The following example shows the minimum and maximum integer values that can be represented as Number object (for details, refer to ECMAScript standard, chapter 6.1.6 The Number Type):

var biggestInt = 9007199254740991;
var smallestInt = -9007199254740991;

When parsing data that has been serialized to JSON, integer values falling out of this range can be expected to become corrupted when JSON parser coerces them to Number type. Using String instead is a possible workaround.

Using Number to convert a Date object

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

var 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('12.3')    // 12.3
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

Specifications

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.1.
ECMAScript 5.1 (ECMA-262)
The definition of 'Number' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Number' in that specification.
Standard New methods and properties added: EPSILON, isFinite, isInteger, isNaN, parseFloat, parseInt
ECMAScript Latest Draft (ECMA-262)
The definition of 'Number' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
EPSILONChrome Full support YesEdge Full support YesFirefox Full support 25IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
MAX_SAFE_INTEGERChrome Full support 34Edge Full support YesFirefox Full support 31IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 31Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
MAX_VALUEChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
MIN_SAFE_INTEGERChrome Full support 34Edge Full support YesFirefox Full support 31IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 31Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
MIN_VALUEChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
NEGATIVE_INFINITYChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
NaNChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
POSITIVE_INFINITYChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
isFiniteChrome Full support 19Edge Full support YesFirefox Full support 16IE No support NoOpera Full support 15Safari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 16Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.10
isIntegerChrome Full support YesEdge Full support YesFirefox Full support 16IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 16Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
isNaNChrome Full support 25Edge Full support YesFirefox Full support 15IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 15Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.10
isSafeIntegerChrome Full support YesEdge Full support YesFirefox Full support 32IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 32Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
parseFloatChrome Full support YesEdge Full support YesFirefox Full support 25IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
parseIntChrome Full support YesEdge Full support YesFirefox Full support 25IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 25Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
prototypeChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toExponentialChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toFixedChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toInteger
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 16 — 32IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support 16 — 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toLocaleStringChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toPrecisionChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox Full support 1IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile 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 YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
valueOfChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

See also