MEPX
Chapter 7 of 16All chapters

Chapter 7 of 16

Numbers and maths

One number type, and what that costs.

Everything is a double

JavaScript has one number type, a 64 bit float, so integers and decimals share it. That is why 0.1 + 0.2 does not print as 0.3, exactly as in other languages.

  • Number.isInteger checks for a whole value, and toFixed formats for display.
  • bigint exists for integers beyond the safe range, written with a trailing n.

Converting

Number("5") and parseInt("5", 10) convert on purpose, which reads better than relying on an operator. Every arithmetic operator except plus converts to numbers, so "5" - 3 gives 2.