Mathematics plays an integral part in computer science and coding. Programmers use mathematical methods and expressions to perform calculations for all sorts of different reasons during development. Luckily, JavaScript provides various built-in methods that can make your life a whole lot easier.
In this article, you'll learn about 19 JavaScript Math methods that you should master today.
1. Math.abs()
The abs() method returns the absolute value of a number.
let num1 = 32;
let num2 = -13;
let num3 = -345;
let num4 = 4.76;
let num5 = 0;
console.log(Math.abs(num1));
console.log(Math.abs(num2));
console.log(Math.abs(num3));
console.log(Math.abs(num4));
console.log(Math.abs(num5));
Output:
32
13
345
4.76
0
2. Math.cbrt()
The cbrt() method returns the cube root of a number.
let num1 = 64;
let num2 = 125;
let num3 = -1;
let num4 = Infinity;
let num5 = 0;
console.log(Math.cbrt(num1));
console.log(Math.cbrt(num2));
console.log(Math.cbrt(num3));
console.log(Math.cbrt(num4));
console.log(Math.cbrt(num5));
Output:
4
5
-1
Infinity
0
3. Math.ceil()
The ceil() method returns the next integer greater than or equal to a given number.
let num1 = 34.5;
let num2 = 54.234;
let num3 = 7.0001;
let num4 = 867.1;
let num5 = 0;
console.log(Math.ceil(num1));
console.log(Math.ceil(num2));
console.log(Math.ceil(num3));
console.log(Math.ceil(num4));
console.log(Math.ceil(num5));
Output:
35
55
8
868
0
4. Math.cos()
The cos() method returns the cosine of the specified angle The given angle must be specified in radians.
let num1 = 0;
let num2 = 1;
let num3 = Math.PI;
let num4 = 0.5;
let num5 = 45;
console.log(Math.cos(num1));
console.log(Math.cos(num2));
console.log(Math.cos(num3));
console.log(Math.cos(num4));
console.log(Math.cos(num5));
Output:
1
0.5403023058681398
-1
0.8775825618903728
0.5253219888177297
5. Math.cosh()
The cosh() method returns the hyperbolic cosine of a number.
let num1 = 0;
let num2 = 1;
let num3 = -1;
let num4 = 2;
let num5 = -2;
console.log(Math.cosh(num1));
console.log(Math.cosh(num2));
console.log(Math.cosh(num3));
console.log(Math.cosh(num4));
console.log(Math.cosh(num5));
Output:
1
1.5430806348152437
1.5430806348152437
3.7621956910836314
3.7621956910836314
6. Math.exp(x)
The exp(x) method returns e^x, where x is the argument, and e is Euler's number (also known as Napier's constant), the base of the natural logarithms.
let num1 = 0;
let num2 = 1;
let num3 = -1;
let num4 = 2;
let num5 = -2;
console.log(Math.exp(num1));
console.log(Math.exp(num2));
console.log(Math.exp(num3));
console.log(Math.exp(num4));
console.log(Math.exp(num5));
Output:
1
2.718281828459045
0.36787944117144233
7.38905609893065
0.1353352832366127
7. Math.floor()
The floor() method returns the next integer less than or equal to a given number.
let num1 = 34.5;
let num2 = 54.234;
let num3 = 7.0001;
let num4 = 867.1;
let num5 = 0;
console.log(Math.floor(num1));
console.log(Math.floor(num2));
console.log(Math.floor(num3));
console.log(Math.floor(num4));
console.log(Math.floor(num5));
Output:
34
54
7
867
0
8. Math.log()
The log() method returns the natural logarithm (base e) of a number.
let num1 = 0;
let num2 = 1;
let num3 = -1;
let num4 = 10;
let num5 = -2;
console.log(Math.log(num1));
console.log(Math.log(num2));
console.log(Math.log(num3));
console.log(Math.log(num4));
console.log(Math.log(num5));
Output:
-Infinity
0
NaN
2.302585092994046
NaN
9. Math.max(x, y, ...)
The max() method returns the highest-valued number from a list of numbers.
let list1 = [24, 645, 678, Infinity];
let list2 = [75, 12, 92, 8];
let list3 = [2947, -435, -123, 0, -Infinity];
console.log(Math.max(...list1));
console.log(Math.max(...list2));
console.log(Math.max(...list3));
console.log(Math.max(1, 2, 3));
console.log(Math.max(234, 46, -23));
Output:
Infinity
92
2947
3
234
10. Math.min(x, y, ...)
The min() method returns the lowest-valued number from a list of numbers.
let list1 = [24, 645, 678, Infinity];
let list2 = [75, 12, 92, 8];
let list3 = [2947, -435, -123, 0, -Infinity];
console.log(Math.min(...list1));
console.log(Math.min(...list2));
console.log(Math.min(...list3));
console.log(Math.min(1, 2, 3));
console.log(Math.min(234, 46, -23));
Output:
24
8
-Infinity
1
-23
11. Math.pow(x, y)
The pow(x, y) method returns the base to the exponent power (x^y).
console.log(Math.pow(1, 10));
console.log(Math.pow(2, 4));
console.log(Math.pow(1, Infinity));
console.log(Math.pow(10, 10));
console.log(Math.pow(-1, 3));
Output:
1
16
NaN
10000000000
-1
12. Math.random()
The random() method returns a random number between 0 and 1 (inclusive of 0, but not 1).
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
Output:
0.00882592466863552
0.11903011517453366
0.9689128269384235
0.8462863261013442
0.4431704664004701
13. Math.round()
The round() method returns the value of a number rounded to the nearest integer.
let num1 = 34.5;
let num2 = 54.234;
let num3 = 7.0001;
let num4 = 867.1;
let num5 = 0;
console.log(Math.round(num1));
console.log(Math.round(num2));
console.log(Math.round(num3));
console.log(Math.round(num4));
console.log(Math.round(num5));
Output:
35
54
7
867
0
14. Math.sin()
The sin() method returns the sine of the specified angle The given angle must be specified in radians.
let num1 = 0;
let num2 = 1;
let num3 = Math.PI;
let num4 = 0.5;
let num5 = 45;
console.log(Math.sin(num1));
console.log(Math.sin(num2));
console.log(Math.sin(num3));
console.log(Math.sin(num4));
console.log(Math.sin(num5));
Output:
0
0.8414709848078965
1.2246467991473532e-16
0.479425538604203
0.8509035245341184
15. Math.sinh()
The sinh() method returns the hyperbolic sine of a number.
let num1 = 0;
let num2 = 1;
let num3 = -1;
let num4 = 2;
let num5 = -2;
console.log(Math.sinh(num1));
console.log(Math.sinh(num2));
console.log(Math.sinh(num3));
console.log(Math.sinh(num4));
console.log(Math.sinh(num5));
Output:
0
1.1752011936438014
-1.1752011936438014
3.626860407847019
-3.626860407847019
16. Math.sqrt()
The sqrt() method returns the square root of a number.
let num1 = 64;
let num2 = 25;
let num3 = -1;
let num4 = Infinity;
let num5 = 144;
console.log(Math.sqrt(num1));
console.log(Math.sqrt(num2));
console.log(Math.sqrt(num3));
console.log(Math.sqrt(num4));
console.log(Math.sqrt(num5));
Output:
8
5
NaN
Infinity
12
17. Math.tan()
The tan() method returns the tangent of a number.
let num1 = 0;
let num2 = 1;
let num3 = Math.PI;
let num4 = 0.5;
let num5 = 45;
console.log(Math.tan(num1));
console.log(Math.tan(num2));
console.log(Math.tan(num3));
console.log(Math.tan(num4));
console.log(Math.tan(num5));
Output:
0
1.5574077246549023
-1.2246467991473532e-16
0.5463024898437905
1.6197751905438615
18. Math.tanh()
The tanh() method returns the hyperbolic tangent of a number.
let num1 = 0;
let num2 = 1;
let num3 = -1;
let num4 = 2;
let num5 = -2;
console.log(Math.tanh(num1));
console.log(Math.tanh(num2));
console.log(Math.tanh(num3));
console.log(Math.tanh(num4));
console.log(Math.tanh(num5));
Output:
0
0.7615941559557649
-0.7615941559557649
0.9640275800758169
-0.9640275800758169
19. Math.trunc(x)
The trunc(x) method returns the integer portion of x, removing any fractional digits.
let num1 = 34.5;
let num2 = 54.234;
let num3 = 7.0001;
let num4 = 867.1;
let num5 = -0.7632;
console.log(Math.trunc(num1));
console.log(Math.trunc(num2));
console.log(Math.trunc(num3));
console.log(Math.trunc(num4));
console.log(Math.trunc(num5));
Output:
34
54
7
867
-0
If you want to have a look at the complete source code used in this article, here's the GitHub repository.
Make Use of Array Methods
Arrays are one of the most used data structures in programming. Along with Math methods, JavaScript also provides several built-in array methods like push(), concat(), join(), forEach(), map(), sort(), and so on. You can make use of all these built-in methods to work comfortably with JavaScript arrays.