Math functions in JavaScript
Today we’ll show you the list of JavaScript Math functions that every web developer should know.
Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.
Checkout more articles on JavaScript
List of JavaScript Math functions
1. Math.min()
The Math.min()
function returns the number with the lowest value.
1 2 3 4 5 6 | Math.min(15, 17, 20, 5); // Output: 5 Math.min(-15, -17, -20, -5); // Output: -20 const list = [15, 17, 20, 5, -15, -17, -20, -5]; Math.min(...list); // Output: -20 |
2. Math.max()
The Math.max()
function returns the number with the largest value.
1 2 3 4 5 6 | Math.max(15, 17, 20, 5); // Output: 20 Math.max(-15, -17, -20, -5); // Output: -5 const list = [15, 17, 20, 5, -15, -17, -20, -5]; Math.max(...list); // Output: 20 |
3. Math.random()
The Math.random()
function returns the random number between 0 to 1.
1 2 3 4 5 | Math.random(); // Output: 0.18912103331760988 Math.random(); // Output: 0.20911447024845153 Math.random(); // Output: 0.31503359473699244 |
4. Math.round()
The Math.round()
function returns the rounds number to its nearest integer.
1 2 3 4 5 6 7 | Math.round(15.05); // Output: 15 Math.round(15.50); // Output: 16 Math.round(-15.05); // Output: -15 Math.round(-15.50); // Output: -15 |
5. Math.ceil()
The Math.ceil()
function rounds a number up to the next largest integer.
1 2 3 4 5 6 7 | Math.ceil(15.05); // Output: 16 Math.ceil(15.50); // Output: 16 Math.ceil(-15.05); // Output: -15 Math.ceil(-15.50); // Output: -15 |
6. Math.pow()
The Math.pow()
function returns the base to the exponent power.
1 2 3 4 5 6 7 | Math.pow(2, 3); // Output: 8 Math.pow(3, 2); // Output: 9 Math.pow(7, 2); // Output: 49 Math.pow(9, 2); // Output: 81 |
7. Math.floor()
The Math.floor()
function rounds a number down to its nearest integer.
1 2 3 4 5 6 7 | Math.floor(15.05); // Output: 15 Math.floor(15.50); // Output: 15 Math.floor(-15.05); // Output: -16 Math.floor(-15.50); // Output: -16 |
8. Math.abs()
The Math.abs()
function returns the absolute value of a number.
1 2 3 4 5 6 7 | Math.abs(15); // Output: 15 Math.abs('-15'); // Output: 15 Math.abs(-15); // Output: 15 Math.abs(null); // Output: 0 |
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂