Javascript is a built-in-object that provides functions and methods for mathematical calculation . Math is not a function or a constructor , but we can call Math as an Object .
Javascript Math is having number of functions and properties. Let us see them in detail:-
Math.round():-
This method will round up the given number to the nearest integer. If the given number is an integer it will directly return the given number.
Syntax:- Math.round(num)
Example:-
Math.round(6.5) // output :- 7
Math.round(6.2) // output :- 6
Math.round(5) // output :- 5
Math.pow():-
This method will return the value after multiplying the base with itself the exponent times.
Syntax:- Math.pow(base,exponent)
Example:-
Math.pow(2,3) // output :- 8
Math.round(6,6) // output :- 46656
Math.round(5,3) // output :- 125
Math.sqrt():-
The square root of an integer is returned by the sqrt() JavaScript math method . It returns NaN if the supplied number is negative.
Syntax:- Math.sqrt(num)
Example:-
Math.sqrt(4) // output :- 2
Math.sqrt(25) // output :- 5
Math.sqrt(64) // output :- 8
Math.sqrt(-4) // output : NAN
Math.abs():-
The abs() will return the absolute value of a given number.
Syntax:- Math.abs(num)
Example:-
Math.abs(-4) // output :- 4
Math.abs('data') // output :- NaN
Math.abs('64') // output :- 64
Math.abs('') // output : 0
Math.ceil():-
the ceil() method will increase the given number to the closed largest number and if the given number is integer then it will return the number directly
Syntax:- Math.ceil(num)
Example:-
Math.ceil(0.2) // output :- 1
Math.ceil(6.4) // output :- 7
Math.ceil(-0.2) // output :- 0
Math.ceil(-6.4) // output : -6
Math.floor():-
the floor() method will decrease the given number to the closed smallest number and if the given number is an integer then it will return the number directly
Syntax:- Math.floor(num)
Example:-
Math.floor(0.2) // output :- 0
Math.floor(6.4) // output :- 6
Math.floor(-0.2) // output :- -1
Math.floor(-6.4) // output : -7
Math.sin():-
the sin() method will return the sine of the given number . the return value will be in between -1 to 1
Syntax:- Math.floor(num)
Example:-
Math.sin(0.2) // output :- 0.19866933079506122
Math.sin(6.4) // output :- 0.11654920485049364
Math.sin(-0.2) // output :- -0.19866933079506122
Math.sin(-6.4) // output : -0.11654920485049364
Math.cos():-
the cos() method will return the cosine of the given number. the return value will be in between -1 to 1
Syntax:- Math.floor(num)
Example:-
Math.cos(0.2) // output :- 0.9800665778412416
Math.cos(6.4) // output :- 0.9931849187581926
Math.cos(-0.2) // output :- 0.9800665778412416
Math.cos(-6.4) // output : 0.9931849187581926
Math.min() and Math.max():-
the min() method will return the minimum number from the given numbers whereas the max() method will return the maximum number from all the given values.
Syntax:- Math.max(num1,num2 ,………num n ) and Math.min(num1,num2 ,………num n )
Example:-
Math.min(-10,-24,-12,-20, 22,34,12,15) // output :- -24
Math.max(-10,-24,-12,-20, 22,34,12,15) // output :- 34
Math.random():-
The random() method will return the random number from 0 to 1 where 0 is included in the given range and 1 is not included
Syntax:- Math.random()
Example:-
Math.random() // output :- 0.7541912847957812
These are just a few examples, there are many other math functions available in JavaScript.
Thanks for reading hope this article helps you .