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:
console.log(Math.round(9.6)); console.log(Math.round(9.2)); console.log(Math.round(10.5));
10 9 11
| Function | What it does | Example | Result |
|---|---|---|---|
| Math.round | rounds to the nearest whole number | Math.round(10.5) | 11 |
| Math.floor | drops the fraction (rounds down) | Math.floor(10.5) | 10 |
| Math.max | the largest value | Math.max(5, 10, 50) | 50 |
| Math.min | the smallest value | Math.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:
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));
7 92 78
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:
console.log(Math.random()); console.log(Math.floor(Math.random() * 10));
0.000054784119180202595 9
You can also use a random number in a condition, like tossing a coin:
if (Math.random() >= 0.5) {
console.log("Lucky today");
} else {
console.log("Unlucky today");
}Unlucky today
Practice
Round 9.7 with Math.round and print the result.
Press Run to see the result. Print 5.5 rounded with Math.round, then 8.8 with its fraction dropped using Math.floor.
Press Run to see the result. Print the larger of 10 and 20 with Math.max, then the smaller with Math.min.
Press Run to see the result. Complete the line so it prints a random whole number from 0 to 9: multiply Math.random() by 10, then drop the fraction.
console.log(___);
Press Run to see the result. Complete the condition: if the random number is greater than or equal to 0.5, print Excellent luck, otherwise Bad luck.
if (___) {
console.log("Excellent luck");
} else {
console.log("Bad luck");
}Press Run to see the result. 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.
Press Run to see the result. 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.
Hint
47 ÷ 12 = 3.9..., then drop the fraction.let students = 47; let seats = 12;
Press Run to see the result. Key points
- A function takes an argument in brackets and returns a value.
Math.roundrounds,Math.floordrops the fraction,Math.max/Math.mingive the largest and smallest value.Math.floor(Math.random() * 10)is a random whole number from 0 to 9.