The Number
JavaScript object is a wrapper object allowing you to work with numerical values. A Number
object is created using the Number()
constructor. A primitive type object number is created using the Number() function.
Syntax
new Number(value); var a = new Number('123'); // a === 123 is false var b = Number('123'); // b === 123 is true a instanceof Number; // is true b instanceof Number; // is false
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)
and253 - 1
). Number.toInteger()
Used to evaluate the passed value and convert it to an integer (orInfinity
), 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 theObject.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('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
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
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Number | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
EPSILON | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 25 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
MAX_SAFE_INTEGER | Chrome Full support 34 | Edge Full support Yes | Firefox Full support 31 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
MAX_VALUE | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
MIN_SAFE_INTEGER | Chrome Full support 34 | Edge Full support Yes | Firefox Full support 31 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
MIN_VALUE | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
NEGATIVE_INFINITY | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
NaN | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
POSITIVE_INFINITY | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
isFinite | Chrome Full support 19 | Edge Full support 12 | Firefox Full support 16 | IE No support No | Opera Full support 15 | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 16 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.10 |
isInteger | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 16 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 16 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
isNaN | Chrome Full support 25 | Edge Full support 12 | Firefox Full support 15 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 15 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.10 |
isSafeInteger | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 32 | IE No support No | Opera Full support Yes | Safari Full support 10 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 32 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
parseFloat | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
parseInt | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
prototype | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
toExponential | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
toFixed | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
toInteger | Chrome No support No | Edge No support No | Firefox No support 16 — 32 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Edge Mobile No support No | Firefox Android No support 16 — 32 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toLocaleString | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
toPrecision | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
toSource | Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Edge Mobile No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
valueOf | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs 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
NaN
- The
Math
global object - Number type in details