Math.min method
The Math.min method returns
the smallest number from the group
of numbers passed as parameters. If
nothing is passed as parameters,
then Infinity will be returned.
Syntax
Math.min(number, number, number...);
Example
Let's output the minimum number from a group of numbers:
console.log(Math.min(40, 20, 42, 100, 67));
The code execution result:
20
Example
Let's output the minimum number from a group of numbers:
console.log(Math.min(-1, -100, -30, -25, 40));
The code execution result:
-100
Example
The following example will output
Infinity because no parameters
were passed to the method:
console.log(Math.min());
The code execution result:
Infinity
Example
By default, the function does not work with arrays. However, it can be made to do so with the spread operator. Let's use it to display the smallest value of the array:
let arr = [1, 5, 10, 34, 100];
let min = Math.min(...arr);
console.log(min);
The code execution result:
1
Example
You can also make the function work
with arrays using the
apply
method:
let arr = [1, 5, 10, 34, 100];
let min = Math.min.apply(null, arr);
console.log(min);
The code execution result:
1
See also
-
the
Math.maxmethod
that returns the maximum number from a group of numbers