Understand JavaScript Math.min() method with Example
Last updated on by Digamber
JavaScript Math.min() methods returns the lowest numerical value passed into it. It won’t return any value if passed value is not a numerical value.
Math.min(6, 5, 3, 9, 7);
// output: 3
Sometimes we have to deal with number values with arrays; in this situation, it doesn’t work well. In order to get rid of this issue, we need to use Function.prototype.apply()
.
let num = [2, 3, 4, 5, 6, 7, 8, 9];
Math.min.apply(null, num)
// output: 2
JavaScript Math.min() Method Syntax
Math.min(a1, a2, a3, ..., aX)
Argument | Detail |
---|---|
a1, a2, a3, …, aX | In order to get the lowest value, pass numerical values here. It’s optional as well. |
Math.min() with Numerical Array using Spread Operator
Thanks to ECMAScript 2015, It can easily be done with ECMAScript spread operator. Check out the solution below.
let num = [2, 3, 4, 5, 6, 7, 8, 9];
Math.min(...num)
// output: 2
The new ECMAScript spread operator expands the values into the method’s parameter.
JavaScript Min Method Examples
let a = Math.min(6, 13);
// output: 6
let b = Math.min(0, 120, 20, 30, 58);
// output: 0
let c = Math.min(-3, 40);
// output: -3
let d = Math.min(-6, -12);
// output: -12
let e = Math.min(4.2, 3.2);
// output: 3.2
Math.min() Browser Support
Chrome | Firefox | Edge | Safari | Opera |
---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes |
Detailed browser compatibility report on MDN Web Docs