Math
is a built-in object in JavaScript that comes with various mathematical constants and methods, which allows you to perform more complex mathematical tasks that the basic arithmetic operators cannot. For example,
1console.log(Math.E); // returns Euler's number
2console.log(Math.PI); // returns PI
3console.log(Math.SQRT2); // returns the square root of 2
4console.log(Math.SQRT1_2); // returns the square root of 1/2
5console.log(Math.LN2); // returns the natural logarithm of 2
6console.log(Math.LN10); // returns the natural logarithm of 10
7console.log(Math.LOG2E); // returns base 2 logarithm of E
8console.log(Math.LOG10E); // returns base 10 logarithm of E
12.718281828459045
23.141592653589793
31.4142135623730951
40.7071067811865476
50.6931471805599453
62.302585092994046
71.4426950408889634
80.4342944819032518
Besides the constants, there are also a few built-in Math
methods. For instance, the abs()
method returns the absolute value of a given number.
1console.log(Math.abs(10));
2console.log(Math.abs(-10));
110
210
Notice that unlikeDate()
,Math
is an object, not a constructor.
Most of these methods are only useful when performing math-related tasks, and based on their names, you can probably guess what they do.
For this lesson, we are only going to cover some of the methods that are more common for daily uses.
Rounding numbers
Let's start with the methods used for rounding numbers. Four different methods are available for this purpose: ceil()
, floor()
, round()
, and trunc()
.
ceil()
is used to round a number up to its nearest integer.
1console.log(Math.ceil(1.1));
2console.log(Math.ceil(1.5));
3console.log(Math.ceil(2.6));
4console.log(Math.ceil(2.1));
12
22
33
43
floor()
rounds a number down to its nearest integer.
1console.log(Math.floor(1.1));
2console.log(Math.floor(1.5));
3console.log(Math.floor(2.6));
4console.log(Math.floor(2.1));
11
21
32
42
round()
rounds a number to its nearest integer, up or down.
1console.log(Math.round(1.1));
2console.log(Math.round(1.5));
3console.log(Math.round(2.6));
4console.log(Math.round(2.1));
11
22
33
42
trunc()
keeps only the integer part of the given number, ignoring the decimals.
1console.log(Math.trunc(1.1));
2console.log(Math.trunc(1.5));
3console.log(Math.trunc(2.6));
4console.log(Math.trunc(2.1));
11
21
32
42
Min and max
There are also two methods available under the Math
object that allow you to get the maximum or the minimum number from a list of options. For example,
1console.log(Math.min(10, 23, 45, 1.23, -9.81, 2)); // Getting the minimum number from a list of options
2console.log(Math.max(10, 23, 45, 1.23, -9.81, 2)); // Getting the maximum number from a list of options
1-9.81
245
These two methods are often combined with the spread syntax, which allows you to quickly retrieve the maximum or the minimum number from an array.
1const arr = [10, 23, 45, 1.23, -9.81, 2];
2
3console.log(Math.min(...arr));
4console.log(Math.max(...arr));
1-9.81
245
Generating a random number
Lastly, the random()
method is used to generate a random number from 0 to 1.
1for (let i = 0; i < 10; i++) {
2 console.log(Math.random());
3}
10.5005385675122309
20.803443864216842
30.4949878692761194
40.7837324718622103
50.2582257930452574
60.6238402226192539
70.5597391514522028
80.5409924511452422
90.026266338092775898
100.011786527821018078
When combined with the rounding methods we just introduced, you can use random()
to generate random integers as well. For instance, the following example generates a random integer from 0 to 100:
1for (let i = 0; i < 10; i++) {
2 console.log(Math.round(Math.random() * 100));
3}
132
256
343
489
510
699
78
855
960
1087