JavaScript Math Functions
The math function helps us to perform mathematical operations on numbers.
Hello Guys, Welcome to Coding Torque! In this blog, I am going to share 9 commonly used JavaScript Functions. Almost 98% of websites in the world use JavaScript. Let's get started 🚀.
1. Math.random(): This method returns a random number between 0(inclusive) and 1(Exclusive).
console.log(Math.random()) //Output: 0.9935936579868072
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
3. Math.sqrt(): This method returns the square root of a given number.
console.log(Math.sqrt(361)) //Output: 19
console.log(Math.sqrt(64)) //Output: 8
4. Math.floor(): This method returns a value of a 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
5. Math.ceil(): This method returns a value of a 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
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
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
8. Math.min(): This method returns the 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
9. Math.max(): This method returns the 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