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 consistently stores numbers as double-precision floating-point, in accordance with the international IEEE 754 standard.
So, an apparent integer is implicitly a floating-point number!
There is, like in most programming languages, unavoidable round-off errors:
Number literals
Decimal numbers: 42, 0.42
Exponential notation: 420e-1
Hexadecimal: 0x2A
Binary: 0b101010
Octal: 0o52
Special Values
JavaScript has the special values Infinity and -Infinity:
There is also NaN, which means “Not a Number.” You get this when, e.g., number cannot be parsed from a string or when a Math operation result is not a real number.
You also get NaN when you do weird stuff!
For a reference, visit MDN web docs on Infinity and NaN.
Number Wrapper object
You can borrow many useful methods defined in Number object. For instance, there is a toString method with optional base argument (between 2 and 36):
The toPrecision and toExponential can be used for formatting:
The Number object has several useful static method and constants as well. For example, there is parseInt method with optional base argument (between 2 and 36):
There is parseFloat method to convert floating point to number:
JavaScript number type cannot represent integers bigger than 253−1 (Number.MAX_SAFE_INTEGER constant). For integers larger than that limit, you can use a special built-in object, BigInt.
BigInt can be used to represent arbitrary large integers.
BigInt literal have suffix n:
You can also create BigInt by calling BigInt() function: