JavaScript Tutorial
JavaScript Numbers
JavaScript has one number type for integers and floats. NaN and Infinity are values, not crashes.
One type for all ordinary numbers
Python splits integers and floats. JavaScript does not. 42 and 3.14 are bothtypeof "number". There is no separate integer type for everyday counting and money.
Write whole numbers without a decimal. Write fractions with a dot. Scientific notation such as1.5e3 is still a number: it means 1500.
Run every example in /javascript/try. console.log lands in the console under the page.
Integers and floats
Both of these values have the same type. Arithmetic works on either. Dividing two whole numbers can produce a fraction: 5 / 2 is 2.5, not 2.
Example
const whole = 42;
const fraction = 3.14;
const scientific = 2.5e2;
console.log(typeof whole);
console.log(typeof fraction);
console.log(scientific);
console.log(5 / 2);
document.body.textContent =
typeof whole + " " + typeof fraction + " " + scientific + " " + (5 / 2);
NaN is a value
NaN means “not a number.” It is still typed as "number". You get it from math that does not make sense: Number("hello"), 0 / 0, or Math.sqrt(-1).
The program does not stop. Later math with NaN stays NaN. That is why a total can become blank or “NaN” on the page if an input was not numeric.
Example
const bad = Number("hello");
console.log(bad);
console.log(typeof bad);
console.log(0 / 0);
console.log(bad + 10);
document.body.textContent = String(bad) + " " + typeof bad + " " + (bad + 10);
NaN === NaN is false. Use Number.isNaN(value) to test. The olderisNaN(value) coerces first, so isNaN("hello") is true for the wrong reason.
Infinity is a value
Divide a positive number by 0 and you get Infinity. Divide a negative number by0 and you get -Infinity. Again, this is not a crash. It is a number you can store, print, and compare.
Example
console.log(1 / 0);
console.log(-1 / 0);
console.log(Number.POSITIVE_INFINITY);
console.log(1 / 0 === Infinity);
document.body.textContent = String(1 / 0) + " " + String(-1 / 0);
Precision has a limit
JavaScript numbers are 64-bit floating point. Whole numbers are exact up toNumber.MAX_SAFE_INTEGER (9007199254740991). Past that, some integers cannot be represented and look equal when they are not.
Tiny fractions also surprise you: 0.1 + 0.2 is not exactly 0.3. For money, store cents as integers, or round with the methods in the next chapter.
Example
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740993 === 9007199254740992);
document.body.textContent = String(0.1 + 0.2);
Special number values
| Value | How you get it | typeof |
|---|---|---|
42 | A literal integer | "number" |
3.14 | A literal float | "number" |
NaN | Failed numeric work | "number" |
Infinity | Overflow or divide by zero | "number" |
bigint exists for huge integers, written with an n suffix such as10n. You do not need it for this track. Mix a bigint with a number and JavaScript throws.
What to remember
- One type covers integers and floats.
NaNandInfinityare values. The script keeps running.- Test with
Number.isNaN, not=== NaN. - Do not trust tiny decimal sums for money without rounding.
Next: methods that turn numbers into text and text back into numbers.