12.5. Math Methods¶
As with strings and arrays, JavaScript provides some builtin methods for the
Math
object. These allow us to perform calculations or tasks that are more
involved than simple multiplication, division, addition, or subtraction.
12.5.1. Common Math Methods¶
The Math
object contains over 30 methods. The table below provides a sample
of the most frequently used options. More complete lists can be found here:
To see detailed examples for a particular method, click on its name.
Method 
Syntax 
Description 


Returns the positive value of 


Rounds the decimal 


Rounds the decimal 


Returns the largest value from a set of numbers. 


Returns the smallest value from a set of numbers. 


Returns the value of x raised to the power of y (x ^{y}). 


Returns a random decimal value between 0 and 1, NOT including 1. 


Returns 


Returns the square root of 


Removes any decimals and returns the integer part of 
12.5.2. Check Your Understanding¶
Follow the links in the table above for the floor
, random
, round
,
and trunc
methods. Review the content and then answer the following
questions.
Question
Which of the following returns 3
when applied to 3.87
?
Math.floor(3.87)
Math.random(3.87)
Math.round(3.87)
Math.trunc(3.87)
Question
What is printed by the following program?
1 2 3  let num = Math.floor(Math.random()*10);
console.log(num);

A random number between 0 and 9.
A random number between 0 and 10.
A random number between 1 and 9.
A random number between 1 and 10.
Question
What is printed by the following program?
1 2 3  let num = Math.round(Math.random()*10);
console.log(num);

A random number between 0 and 9.
A random number between 0 and 10.
A random number between 1 and 9.
A random number between 1 and 10.