JavaScript Math functions
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.
- One-Liners JavaScript code that you should know
- Trim all properties of an object in JavaScript
- How to add days to a date in JavaScript
- How to Copy to Clipboard in JavaScript
- How to stash changes in Git
1. Math.min()
The Math.min()
function returns the number with the lowest value.
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.
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.
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.
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.
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.
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.
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.
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..!!