19 JavaScript math methods you should master today

0


Mathematics is an integral part of computing and coding. Programmers use mathematical methods and expressions to perform calculations for all kinds of different reasons during development. Fortunately, JavaScript provides various built-in methods that can make your life easier.

In this article, you’ll learn 19 JavaScript math methods you should master today.

1. Math.abs ()

the abs () The 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));

To go out:

32
13
345
4.76
0

2. Math.cbrt ()

the cbrt () The 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));

To go out:

4
5
-1
Infinity
0

3. Math.ceil ()

the ceiling() The 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));

VIDEO OF THE DAY MAKEUSE

To go out:

35
55
8
868
0

4. Math.cos ()

the cos () The 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));

To go out:

1
0.5403023058681398
-1
0.8775825618903728
0.5253219888177297

5. Math.cosh ()

the baton() The 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));


To go out:

1
1.5430806348152437
1.5430806348152437
3.7621956910836314
3.7621956910836314

6. Math.exp (x)

the exp (x) the method returns e ^ x, where X is the argument, and e is the Euler number (also known as the Napier constant), the base of 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));

To go out:

1
2.718281828459045
0.36787944117144233
7.38905609893065
0.1353352832366127

7. Math.floor ()

the stage() The 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));

To go out:

34
54
7
867
0

Related: JavaScript Map Methods You Need To Master Today

8. Math.log ()

the Newspaper() The 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));

To go out:

-Infinity
0
NaN
2.302585092994046
NaN

9. Math.max (x, y, …)

the max () The method returns the highest value 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));

To go out:

Infinity
92
2947
3
234

Related: JavaScript Bundle Methods You Must Master Today

10. Math.min (x, y, …)

the min () The method returns the lowest number in 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));

To go out:

24
8
-Infinity
1
-23

11. Math.pow (x, y)

the pow (x, y) The method returns the base to the power of the exponent (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));

To go out:

1
16
NaN
10000000000
-1

12. Random Math ()

the Random() The method returns a random number between 0 and 1 (including 0, but not 1).

console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());

To go out:

0.00882592466863552
0.11903011517453366
0.9689128269384235
0.8462863261013442
0.4431704664004701

Related: JavaScript String Methods You Should Master Today

13. Math.round ()

the tower() The 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));

To go out:

35
54
7
867
0

14. Math.sin ()

the peach() The 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));


To go out:

0
0.8414709848078965
1.2246467991473532e-16
0.479425538604203
0.8509035245341184

15. Math.sinh ()

the sinh () The 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));

To go out:

0
1.1752011936438014
-1.1752011936438014
3.626860407847019
-3.626860407847019

16. Math.sqrt ()

the sqrt () The 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));

To go out:

8
5
NaN
Infinity
12

Related: One-Liners JavaScript You Need To Know

17. Math.tan ()

the tan () The 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));

To go out:

0
1.5574077246549023
-1.2246467991473532e-16
0.5463024898437905
1.6197751905438615

18. Math.tanh ()

the tanh () The 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));

To go out:

0
0.7615941559557649
-0.7615941559557649
0.9640275800758169
-0.9640275800758169

19. Math.trunc (x)

the trunk (x) The method returns the integer part 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));

To go out:

34
54
7
867
-0

If you’d like to check out the full source code used in this article, here’s the GitHub repository.

Use array methods

Arrays are one of the most used data structures in programming. In addition to math methods, JavaScript also provides several built-in array methods such as to push(), concatenated (), to rejoin(), for each(), card(), sort (), etc. You can use all of these built-in methods to comfortably work with JavaScript tables.



javascript-array-methods

15 JavaScript array methods you should master today

Want to understand JavaScript tables but can’t seem to master them? See our sample JavaScript tables for tips.

Read more


About the Author


Share.

About Author

Comments are closed.