--- title: Number slug: Web/JavaScript/Reference/Global_Objects/Number translation_of: Web/JavaScript/Reference/Global_Objects/Number ---
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 {{jsxref("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 {{jsxref("BigInt")}} type, but it was not designed to replace Number for everyday uses. 37
is still a Number, not a BigInt.)
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 {{jsxref("NaN")}}.
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
Number()
Number
value.253 - 1
).-(253 - 1)
).Number
object.NaN
.-(253 - 1)
and 253 - 1
).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
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 {{jsxref("String")}} instead.
Larger numbers can be represented using the {{jsxref("BigInt")}} type.
The following example converts the {{jsxref("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
.
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
Specification |
---|
{{SpecName('ESDraft', '#sec-number-objects', 'Number')}} |
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.
{{Compat("javascript.builtins.Number")}}