DEV Community

Piyush | Coding Torque
Piyush | Coding Torque

Posted on

09 Common JavaScript Math Functions

The Math Function helps us to perform Mathematical Tasks on numbers.

These are 9 Most Common JavaScript Math functions you must know👇.

1. Math.random() : This Method returns a random number between 0(inclusive) and 1(Exclusive).

console.log(Math.random()) //Output: 0.9935936579868072
Enter fullscreen mode Exit fullscreen mode

2. Math.pow() : This Method takes two parameters like Math.pow(x,y) and returns a value of x to the power y.

console.log(Math.pow(5,2)) //Output: 25
console.log(Math.pow(12,3)) //Output: 1728
Enter fullscreen mode Exit fullscreen mode

3. Math.sqrt() : This Method returns square root of given number.

console.log(Math.sqrt(361)) //Output: 19
console.log(Math.sqrt(64)) //Output: 8
Enter fullscreen mode Exit fullscreen mode

4. Math.floor() : This Method returns a value of given number down to its nearest number.

console.log(Math.floor(12.34)) //Output: 12
console.log(Math.floor(10.94)) //Output: 10
console.log(Math.floor(-5.34)) //Output: -6
console.log(Math.floor(-4.59)) //Output: -5
Enter fullscreen mode Exit fullscreen mode

5. Math.ceil() : This Method returns a value of given number up to its nearest number.

console.log(Math.ceil(20.34)) //Output: 21
console.log(Math.ceil(30.94)) //Output: 31
console.log(Math.ceil(-3.55)) //Output: -3
console.log(Math.ceil(-6.01)) //Output: -6
Enter fullscreen mode Exit fullscreen mode

6. Math.trunc() : It returns only the integer part of the given number by removing fractional units.

console.log(Math.trunc(0.12)) //Output: 0
console.log(Math.trunc(5.12)) //Output: 5
console.log(Math.trunc(-4.12)) //Output: -4
console.log(Math.trunc(40.12)) //Output: 40
Enter fullscreen mode Exit fullscreen mode

7. Math.abs() : This method returns the absolute value i.e Positive Value of a given number.

console.log(Math.abs(1.12)) //Output: 1
console.log(Math.abs(-1)) //Output: 1
console.log(Math.abs(-4.12)) //Output: 4
console.log(Math.abs(-30.12)) //Output: 30
Enter fullscreen mode Exit fullscreen mode

8. Math.min() : This method returns a lowest value from a provided list of numeric values.

console.log(Math.min(2,4,6,7,8,9,1,6,34)) //Output: 1
console.log(Math.min(12,24,36,48,60)) //Output: 12
Enter fullscreen mode Exit fullscreen mode

9. Math.max() : This method returns a highest value from a provided list of numeric values.

console.log(Math.max(2,4,6,7,8,9,1,6,34)) //Output: 34
console.log(Math.max(12,24,36,48,60)) //Output: 60
Enter fullscreen mode Exit fullscreen mode

Top comments (0)