Number values represent floating-point numbers like
Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the
Numbers are most commonly expressed in literal forms like
3.14159. The lexical grammar contains a more detailed reference.
255; // two-hundred and fifty-five 255.0; // same number 255 === 255.0; // true 255 === 0xff; // true (hexadecimal notation) 255 === 0b11111111; // true (binary notation) 255 === 0.255e3; // true (decimal exponential notation)
A number literal like
BigInt type, but it's 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
Number("123"); // returns the number 123 Number("123") === 123; // true Number("unicorn"); // NaN Number(undefined); // NaN
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 the stored number's magnitude and precision. Very briefly, an IEEE 754 double-precision number uses 64 bits to represent 3 parts:
- 1 bit for the sign (positive or negative)
- 11 bits for the exponent (-1022 to 1023)
- 52 bits for the mantissa (representing a number between 0 and 1)
The mantissa (also called significand) is the part of the number representing the actual value (significant digits). The exponent is the power of 2 that the mantissa should be multiplied by. Thinking about it as scientific notation:
The mantissa is stored with 52 bits, interpreted as digits after
1.… in a binary fractional number. Therefore, the mantissa's precision is 2-52 (obtainable via
Number.EPSILON), or about 15 to 17 decimal places; arithmetic above that level of precision is subject to rounding.
The largest value a number can hold is 21024 - 1 (with the exponent being 1023 and the mantissa being 0.1111… in base 2), which is obtainable via
Number.MAX_VALUE. Values higher than that are replaced with the special number constant
Integers can only be represented without loss of precision in the range -253 + 1 to 253 - 1, inclusive (obtainable via
Number.MAX_SAFE_INTEGER), because the mantissa can only hold 53 bits (including the leading 1).
More details on this are described in the ECMAScript standard.
Many built-in operations that expect numbers first coerce their arguments to numbers (which is largely why
Number objects behave similarly to number primitives). The operation can be summarized as follows:
- Numbers are returned as-is.
- Strings are converted by parsing them as if they contain a number literal. Parsing failure results in
NaN. There are some minor differences compared to an actual number literal:
- Leading and trailing whitespace/line terminators are ignored.
- A leading
0digit does not cause the number to become an octal literal (or get rejected in strict mode).
-are allowed at the start of the string to indicate its sign. (In actual code, they "look like" part of the literal, but are actually separate unary operators.) However, the sign can only appear once, and must not be followed by whitespace.
-Infinityare recognized as literals. In actual code, they are global variables.
- Empty or whitespace-only strings are converted to
- Numeric separators are not allowed.
- BigInts throw a
TypeErrorto prevent unintended implicit coercion causing loss of precision.
- Symbols throw a
- Objects are first converted to a primitive by calling their
toString()methods, in that order. The resulting primitive is then converted to a number.
- Unary plus:
+xdoes exactly the number coercion steps explained above to convert
Number(x)uses the same algorithm to convert
x, except that BigInts don't throw a
TypeError, but return their number value, with possible loss of precision.
Number.parseInt() are similar to
Number() but only convert strings, and have slightly different parsing rules. For example,
parseInt() doesn't recognize the decimal point, and
parseFloat() doesn't recognize the
Some operations expect integers, most notably those that work with array/string indices, date/time components, and number radixes. After performing the number coercion steps above, the result is truncated to an integer (by discarding the fractional part). If the number is ±Infinity, it's returned as-is. If the number is
-0, it's returned as
0. The result is therefore always an integer (which is not
-0) or ±Infinity.
Notably, when converted to integers, both
undefined is converted to
NaN, which also becomes
Fixed-width number conversion
TypedArray objects. Bitwise operators always convert the operands to 32-bit integers. In these cases, after converting the value to a number, the number is then normalized to the given width by first truncating the fractional part and then taking the lowest bits in the integer's two's complement encoding.
new Int32Array([1.1, 1.9, -1.1, -1.9]); // Int32Array(4) [ 1, 1, -1, -1 ] new Int8Array([257, -257]); // Int8Array(2) [ 1, -1 ] // 257 = 0001 0000 0001 // = 0000 0001 (mod 2^8) // = 1 // -257 = 1110 1111 1111 // = 1111 1111 (mod 2^8) // = -1 (as signed integer) new Uint8Array([257, -257]); // Uint8Array(2) [ 1, 255 ] // -257 = 1110 1111 1111 // = 1111 1111 (mod 2^8) // = 255 (as unsigned integer)
Creates a new
Number is called as a constructor (with
new), it creates a
Number object, which is not a primitive. For example,
typeof new Number(42) === "object", and
new Number(42) !== 42 (although
new Number(42) == 42).
Warning: You should rarely find yourself using
Number as a constructor.
The smallest interval between two representable numbers.
The largest positive representable number.
The smallest positive representable number—that is, the positive number closest to zero (without actually being zero).
Special "Not a Number" value.
Special value representing negative infinity. Returned on overflow.
Special value representing infinity. Returned on overflow.
Determine whether the passed value is a finite number.
Determine whether the passed value is an integer.
Determine whether the passed value is
Determine whether the passed value is a safe integer (number between -(253 - 1) and 253 - 1).
This is the same as the global
This is the same as the global
These properties are defined on
Number.prototype and shared by all
The constructor function that created the instance object. For
Numberinstances, the initial value is the
Returns a string representing the number in exponential notation.
Returns a string representing the number in fixed-point notation.
Returns a string with a language sensitive representation of this number. Overrides the
Returns a string representing the number to a specified precision in fixed-point or exponential notation.
Returns a string representing the specified object in the specified radix ("base"). Overrides the
Returns the primitive value of the specified object. Overrides the
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
const biggestInt = Number.MAX_SAFE_INTEGER; // (2**53 - 1) => 9007199254740991 const smallestInt = Number.MIN_SAFE_INTEGER; // -(2**53 - 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
A possible workaround is to use
Larger numbers can be represented using the
Using Number() to convert a Date object
The following example converts the
Date object to a numerical value using
Number as a function:
const d = new Date("1995-12-17T03:24:00"); console.log(Number(d));
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
|ECMAScript Language Specification |
BCD tables only load in the browser
- Polyfill of modern
Numberbehavior (with support binary and octal literals) in
- Arithmetic operators
- Integers with arbitrary precision: