JavaScript Numbers
Written by Avinash Malhotra
Updated on
Numbers in JavaScript
A JavaScript number is a value you can use in a calculation. Whole numbers such as 3 and decimals such as 3.14 both have the same type, number. You can add, subtract, multiply and divide them. The remainder operator (%) gives the amount left over after division.
Write ordinary numbers in decimal (base 10). Use 0b for binary, 0o for octal and 0x for hexadecimal. For example, 0o10 means eight. Older scripts may use 010 for eight. That spelling is a syntax error in strict mode.
Use let for a value that will change, such as a score. Older scripts often use var.
Use const when a name should keep its value. For example, const minutesPerHour = 60. See JavaScript variables.
Use typeof to check the type. For example, typeof 3.14 gives "number", while typeof "3.14" gives "string".
Define Numbers in JS
let a=3; // number;
let b=3.6; // number;
let c=2e3; // exponential number (2000)
let d=0xa; // hexadecimal
let e=010; // octal // (not supported in strict mode)
let f=0o10; // octal in es6
let g=0b100; // binary in es6
let h=NaN; // NaN also has type "number"
let i=Infinity; // positive infinity
let k=1_00_000; // Numeric literal separators
Number()
Number() converts a value to a number. For example, Number("3.5") gives 3.5. If the text cannot be read as a number, the result is NaN, short for “Not a Number”.
Use Number(value) for conversion. Adding new creates a wrapper object, so typeof new Number(3) gives "object".
Number.MAX_VALUE
Number.MAX_VALUE is the largest finite positive value the number type can hold. It is about 1.8 × 10308. Multiplying it by two gives Infinity.
1.7976931348623157e+308
const max=Number.MAX_VALUE;
console.log(max);
Number.MAX_SAFE_INTEGER
Number.MAX_SAFE_INTEGER is 9,007,199,254,740,991, or 253 − 1. Up to this limit, JavaScript can tell every positive whole number apart.
JavaScript stores numbers in a 64-bit format called IEEE 754. It has 53 bits of precision. Beyond the safe integer range, two different whole numbers can round to the same stored value.
Use BigInt when you need an exact whole number outside the safe range.
BigInt
BigInt is a separate JavaScript data type for whole numbers. It can store very large integers exactly. For example, use it when you need to add one to a large counter.
const bigInt = 1234567890123456789012345678901234567890n;
const anotherBigInt = BigInt("1234567890123456789012345678901234567890");
console.log(bigInt + anotherBigInt); // Operations work with BigInt
Add n to a whole number, as in 42n, or pass digits as text to BigInt(). Passing text keeps every digit even when the value is too large for a regular number.
9007199254740991
9007199254740991
const max=Number.MAX_SAFE_INTEGER;
const n = 2 ** 53 - 1;
console.log(max);
console.log(n);
Numbers greater than Number.MAX_SAFE_INTEGER
Some integers above the safe limit are still exact, but there are gaps. In this example, adding 1 and adding 2 to the largest safe integer both give 9007199254740992.
9007199254740991
9007199254740992
9007199254740992 ±1
9007199254740994
9007199254740996 ±1
const max=Number.MAX_SAFE_INTEGER;
console.log(max);
console.log(max+1);
console.log(max+2);
console.log(max+3);
console.log(max+4);
Binary, Octal, Decimal and Hexadecimal Numbers
A number base tells you how many digits a system uses. The table shows the same value in four bases. For example, decimal 10 is written as 1010 in binary and A in hexadecimal.
| Number | Binary (0-1) (base 2) |
Octal (0-7) (base 8) |
Decimal (0-9) (base 10) |
Hexadecimal (0-f) (base 16) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 | 2 |
| 8 | 1000 | 10 | 8 | 8 |
| 10 | 1010 | 12 | 10 | a |
| 15 | 1111 | 17 | 15 | f |
| 16 | 10000 | 20 | 16 | 10 |
Number Methods
These methods return strings you can display. Use toFixed(2) for two digits after the decimal point, such as "3.10". Use toPrecision(2) for two significant digits in total. See the ECMAScript number reference.
| Method | Use | Example |
|---|---|---|
| toString() | Write the number as text. | let x=6; |
| toLocaleString() | Format the number for a chosen language and region. | let x=6; |
| toExponential() | Write the number in scientific notation. | let x=6; |
| toPrecision(1) | Round to one significant digit. | let x=1.23456; |
| toPrecision(2) | Round to two significant digits. | let x=1.23456; |
| toPrecision(3) | Round to three significant digits. | let x=1.23456; |
| toFixed() | Round to a set number of decimal places. | const pi=3.1416; |
How to Convert a String to a Number
A form field gives you text, even when the user enters digits. For example, "5" + 2 gives "52". Convert the text first and Number("5") + 2 gives 7. Learn more about JavaScript strings.
Which conversion function should I use?
Number Function
Use Number() when the whole string should be a number. It accepts spaces at either end. Number("100.5px") gives NaN because of the letters. An empty string or a string of spaces converts to 0.
let a="100";
let b="100.5";
let c="100px";
let d="abc100";
Number(a) //100
Number(b) //100.5
Number(c) //NaN - Not a Number
Number(d) //NaN - Not a Number
Number Function Example
parseInt Function
Use parseInt(text, 10) to read a whole number from the start of decimal text. It skips leading spaces and stops at the first character it cannot use. For example, parseInt("100.5px", 10) gives 100. It drops the decimal part without rounding.
let a="100";
let b="100.5";
let c="100px";
let d="abc100";
parseInt(a) //100
parseInt(b) //100
parseInt(c) //100
parseInt(d) //NaN - Not a Number
The second argument to parseInt() sets the base, also called the radix. Use 2 for binary, 8 for octal, 10 for decimal and 16 for hexadecimal. With no base, the function normally uses 10. A leading 0x or 0X selects base 16.
let a="100";
parseInt(a,2) // 4, binary number
parseInt(a,8) // 64, octal number
parseInt(a,10) // 100, decimal number
parseInt(a,16) // 256, hexadecimal number
parseInt Example
parseFloat Function
Use parseFloat() to read a decimal number from the start of a string. For example, parseFloat("100.5px") gives 100.5. It accepts scientific notation such as "2e3". It reads decimal text only, so parseFloat("0x10") stops at the x and gives 0.
let a="100";
let b="100.5";
let c="100px";
let d="100.5px";
let e="abc100";
parseFloat(a) //100
parseFloat(b) //100.5
parseFloat(c) //100
parseFloat(d) //100.5
parseFloat(e) //NaN - Not a Number
parseFloat Example
isNaN, is Not a Number Function
isNaN(value) first converts its input to a number. It returns true if the result is NaN. For example, isNaN("2") gives false and isNaN("a") gives true.
To check whether a value is already NaN, use Number.isNaN(). For example, Number.isNaN("a") gives false, while Number.isNaN(Number("a")) gives true.
isNaN(NaN) // returns true
isNaN(1) // returns false
isNaN("1") // returns false
isNaN("a") // returns true
isNaN("2a") // returns true
isNaN Example
isFinite
A finite number has a value between -Number.MAX_VALUE and Number.MAX_VALUE, including both ends. The global isFinite() function converts its input before checking. For example, isFinite("2") is true. Both Infinity and NaN give false.
isFinite(Infinity) // false
isFinite(2e308) // false
isFinite(1e308) // true
isFinite(1e307) // true
isFinite(123) // true
isFinite Example
Number.isFinite
Use Number.isFinite(value) when the input must already be a finite number. For example, Number.isFinite(2) is true and Number.isFinite("2") is false.
This check also returns false for booleans, null, NaN and either infinity.
Number.isFinite(1); // true
Number.isFinite('1'); // false
Number.isFinite(true); // false
Number.isFinite(null); // false
- Use an explicit prefix such as
0owhen writing an octal number. - Some decimals cannot be stored exactly in binary. For example,
0.1 + 0.2gives0.30000000000000004.
Summary
Choose the number type, conversion function or formatting method that fits your task.
- Declaration: Use
let,const, orvarfor numbers, including values written with exponents or digit separators. - Number Systems: Binary (0b), Octal (0o), Decimal, Hexadecimal (0x).
- Large Numbers: Use
BigIntfor integers beyondNumber.MAX_SAFE_INTEGER. - Conversion:
Number(),parseInt(),parseFloat()for string-to-number conversion. - Methods:
toString(),toFixed(),toPrecision(),toExponential(). - Validation:
isNaN(),isFinite(),Number.isFinite(). - Limits:
Number.MAX_SAFE_INTEGER(9007199254740991); useBigIntfor larger exact integers.
Try 100.5px in the three conversion examples above. Number() gives NaN, parseInt() gives 100, and parseFloat() gives 100.5.