JavaScript / Functions

Built-in Math functions

Use the ready-made Math functions to round, drop fractions, find the largest and smallest value, and make random numbers.

A function is like a small machine: you give it a value in brackets, called an argument, it does a job and gives back a result, called the return value. JavaScript has many ready-made functions you can use without writing them yourself.

For example, Math.round rounds a number to the nearest whole number:

JavaScript
console.log(Math.round(9.6));
console.log(Math.round(9.2));
console.log(Math.round(10.5));
Console
10
9
11
FunctionWhat it doesExampleResult
Math.roundrounds to the nearest whole numberMath.round(10.5)11
Math.floordrops the fraction (rounds down)Math.floor(10.5)10
Math.maxthe largest valueMath.max(5, 10, 50)50
Math.minthe smallest valueMath.min(5, 10, 50)5

Math.floor is useful when only the whole part matters. Math.max and Math.min take any number of arguments separated by commas, and they can be variables:

JavaScript
console.log(Math.floor(7.8));
let ali = 78;
let sara = 92;
let omar = 85;
console.log(Math.max(ali, sara, omar));
console.log(Math.min(ali, sara, omar));
Console
7
92
78
Write Math with a capital M, then a dot, then the function name. math.round or Math.Round gives an error.

Random numbers

Math.random() takes no argument and returns a random number from 0 up to (not including) 1, different every run. To get a whole number from 0 to 9, multiply by 10, then drop the fraction with Math.floor. Run it a few times:

JavaScript
console.log(Math.random());
console.log(Math.floor(Math.random() * 10));
Console
0.000054784119180202595
9

You can also use a random number in a condition, like tossing a coin:

JavaScript
if (Math.random() >= 0.5) {
  console.log("Lucky today");
} else {
  console.log("Unlucky today");
}
Console
Unlucky today

Practice

1Write the programCurriculum practice

Round 9.7 with Math.round and print the result.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Print 5.5 rounded with Math.round, then 8.8 with its fraction dropped using Math.floor.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

Print the larger of 10 and 20 with Math.max, then the smaller with Math.min.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Complete the line so it prints a random whole number from 0 to 9: multiply Math.random() by 10, then drop the fraction.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log(___);
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

Complete the condition: if the random number is greater than or equal to 0.5, print Excellent luck, otherwise Bad luck.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
if (___) {
  console.log("Excellent luck");
} else {
  console.log("Bad luck");
}
Console
Press Run to see the result.
6Write the programExtra practice

A student scored 78, 85 and 91 in three tests. Work out the average (total ÷ 3), round it with Math.round, then print Average: 85.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

A school trip has 47 students and a minibus holds 12. Use Math.floor to print how many minibuses are completely full: Full buses: 3.

Hint47 ÷ 12 = 3.9..., then drop the fraction.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let students = 47;
let seats = 12;
Console
Press Run to see the result.

Key points

  • A function takes an argument in brackets and returns a value.
  • Math.round rounds, Math.floor drops the fraction, Math.max / Math.min give the largest and smallest value.
  • Math.floor(Math.random() * 10) is a random whole number from 0 to 9.