Math object

The Math object is a way of grouping together math-related constants and functions. In the descriptions below, the nearest integer below or above an integer is the integer itself, and the range [0, 1) includes 0 but not 1.

Name Meaning
Math.PI the constant pi
Math.floor(n) the nearest integer below
Math.ceil(n) the nearest integer above
Math.max(a, b, c) the maximum of the inputs
Math.min(a, b, c) the minimum of the inputs
Math.pow(a, b) a to the power b
Math.sqrt(n) the square root of the input
Math.random() a pseudorandom number in the range [0, 1)

To obtain a pseudorandom integer in the range [0, x), for an integer x, you can use Math.floor(Math.random() * (x + 1)).

Click on the Run button several times to see the different pseudorandom numbers being generated. They will be in the range [0, 10).

Note:

Hints:

  • undefined
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
console.log("The value of pi is " + Math.PI);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX