Web Analytics

JavaScript Numbers & Math

Beginner ~12 min read

JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals.

HTML
CSS
JS
Floating Point Precision: Floating point arithmetic is not always 100% accurate.
0.1 + 0.2 === 0.30000000000000004

Number Methods

Useful methods for working with numbers:

HTML
CSS
JS

The Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers.

HTML
CSS
JS

Random Numbers

Math.random() returns a random number between 0 (inclusive) and 1 (exclusive).

HTML
CSS
JS

Summary

  • JavaScript numbers are always 64-bit floating point.
  • Integers are accurate up to 15 digits.
  • Use Math.round(), Math.ceil(), and Math.floor() to round numbers.
  • Math.random() generates random numbers.

Quick Quiz

Which method rounds a number DOWN to the nearest integer?

A
Math.round()
B
Math.ceil()
C
Math.floor()
D
Math.down()