The Number
JavaScript object is a wrapper object allowing you to work with numerical values. A Number
object is created using the Number()
constructor.
new Number(value);
value
The primary uses of the Number
object are:
NaN
.new
operator), Number
can be used to perform a type conversion.Number.EPSILON
Number.MAX_SAFE_INTEGER
253 - 1
).Number.MAX_VALUE
Number.MIN_SAFE_INTEGER
-(253 - 1)
).Number.MIN_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY
Number.prototype
Number
object.Number.isNaN()
Number.isFinite()
Number.isInteger()
Number.isSafeInteger()
-(253 - 1)
and 253 - 1
).Number.toInteger()
Infinity
), but has been removed.Number.parseFloat()
parseFloat()
of the global object.Number.parseInt()
parseInt()
of the global object.Number
instancesAll Number
instances inherit from Number.prototype
. The prototype object of the Number
constructor can be modified to affect all Number
instances.
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toLocaleString()
Object.prototype.toLocaleString()
method.Number.prototype.toPrecision()
Number.prototype.toSource()
Number
object; you can use this value to create a new object. Overrides the Object.prototype.toSource()
method.Number.prototype.toString()
Object.prototype.toString()
method.Number.prototype.valueOf()
Object.prototype.valueOf()
method.Number
object to assign values to numeric variablesThe 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;
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.
Number
to convert a Date
objectThe 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".
Number('123') // 123 Number('12.3') // 12.3 Number('') // 0 Number('0x11') // 17 Number('0b11') // 3 Number('0o11') // 9 Number('foo') // NaN Number('100a') // NaN
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. | Living Standard |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
NaN
Math
global object
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number