Numbers
JavaScript supports only one type of number, a 64-bit floating-point number. Unlike languages such as Java and C++, JavaScript makes no distinction between integers (whole numbers) and real values (numbers with decimals). It always stores numbers as double-precision floating-point, following the IEEE 754 standard.
let num;
num = 3.14159265;
console.log(typeof num); // number
num = 3;
console.log(typeof num); // number
So even a number that looks like an integer is stored as a floating-point number.
const num = 1 / 2;
console.log(num); // 0.5
Like most programming languages, JavaScript has unavoidable round-off errors:
console.log(0.1 + 0.2); // 0.30000000000000004
Number literals
- Decimal numbers:
42,0.42 - Exponential notation:
420e-1 - Hexadecimal:
0x2A - Binary:
0b101010 - Octal:
0o52
console.log(42); // 42
console.log(420e-1); // 42
console.log(0x2a); // 42
console.log(0b101010); // 42
console.log(0o52); // 42
Special values
JavaScript has the special values Infinity and -Infinity:
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(10 * Number.MAX_VALUE); // Infinity
There is also NaN, which means “Not a Number.” You get this when, for example, a number cannot be parsed from a string, or when the result of a Math operation is not a real number.
console.log(parseInt("blahblah")); // NaN
console.log(Math.sqrt(-1)); // NaN
You also get NaN from operations that have no meaningful numeric result:
console.log(0 * Infinity); // NaN
console.log("JavaScript" / 2); // NaN
Number wrapper object
You can use many methods defined in the Number object. For instance, there is a toString method with optional base argument (between 2 and 36):
const num = 42;
const base = 2;
console.log(num.toString(base)); // 101010
You can use the toPrecision and toExponential methods for formatting:
const num = 3.14159265;
console.log(num.toPrecision(2)); // 3.1
const val = 1000000;
console.log(val.toExponential()); // 1e+6
The Number object has several useful static methods and constants as well. For example, there is a parseInt method with optional base argument (between 2 and 36):
const num = "0x2A"; // Note this is a string!
const base = 16;
console.log(Number.parseInt(num, base)); // 42
There is a parseFloat method to convert a string to a floating-point number:
const num = "0.001"; // Note this is a string!
console.log(Number.parseFloat(num) * 2); // 0.002
There are several methods for type checking:
console.log(Number.isFinite(-1 / 0)); // false
console.log(Number.isNaN(1 / 0)); // false
console.log(Number.isInteger(2.1)); // false
And many useful constants:
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log(Number.POSITIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY); // -Infinity
Big integers
The JavaScript number type cannot safely represent integers bigger than (Number.MAX_SAFE_INTEGER constant). For integers larger than that limit, you can use a special built-in object, BigInt.
BigInt literals have the suffix n:
const num = 18889465931478580854784n;
console.log(typeof num); // bigint
console.log(num * num); // 356811923176489970264571492362373784095686656
You can also create BigInt by calling the BigInt() function:
const num = Number.MAX_SAFE_INTEGER;
console.log(num * num); // 8.112963841460666e+31
console.log(BigInt(num) * BigInt(num)); // 81129638414606663681390495662081
You cannot mix BigInt values and regular numbers in arithmetic operations.
const num = 18889465931478580854784n;
console.log(num + 1); // TypeError: Cannot mix BigInt and other types, use explicit conversions