JavaScript Numbers & Math
JavaScript Numbers
JavaScript has only one type of number. Numbers can be written with or without decimals.
0.1 + 0.2 === 0.30000000000000004
Number Methods
Useful methods for working with numbers:
The Math Object
The JavaScript Math object allows you to perform
mathematical tasks on numbers.
Random Numbers
Math.random() returns a random number between 0
(inclusive) and 1 (exclusive).
Summary
- JavaScript numbers are always 64-bit floating point.
- Integers are accurate up to 15 digits.
- Use
Math.round(),Math.ceil(), andMath.floor()to round numbers. Math.random()generates random numbers.
Quick Quiz
Which method rounds a number DOWN to the nearest integer?
Enjoying these tutorials?