JavaScript ยท Chapter 19 of 55
JavaScript Numbers
JavaScript has a single number type that represents both integers and floating-point decimals, stored as 64-bit floating point values.
Special numeric values include `Infinity`, `-Infinity`, and `NaN` (Not a Number), which results from invalid math operations like dividing zero by zero.
One number type
Unlike many languages, JS doesn't distinguish int vs float โ all numbers are the same type, which simplifies but can also cause precision quirks.
Special values
`NaN` results from invalid operations like `'abc' * 2`. Use `isNaN()` or `Number.isNaN()` to check for it.
Example 1 (javascript)
console.log(typeof 3.14);
console.log(typeof 42);Output
number
numberBoth integers and decimals share the same 'number' type.
Example 2 (javascript)
console.log(10 / 0);
console.log("abc" * 2);Output
Infinity
NaNDivision by zero gives Infinity; invalid math gives NaN.
Key points
- JavaScript has one numeric type for integers and decimals.
- Numbers are stored as 64-bit floating point (IEEE 754).
- `NaN` means 'Not a Number' and results from invalid operations.
- Use `Number.isNaN()` for reliable NaN checks.
๐ก Note: Because of floating-point storage, some decimal math (like 0.1 + 0.2) produces tiny rounding errors.
