Get the Min number of an Array in JavaScript
Get the Min number of an Array
To get the min number of an array, we can use the Math.min() method by passing the array of elements as a argument to it. It returns the min number from the array.
Here is an example:
const arr = [2, 4, 1, 55, 34, 12];
const minNumber = Math.min(...arr);
console.log(minNumber);
Output:
1
In the example above, we have used the Math.min(..arr)
to get the min number of an array.
The Math.min() method accepts only the individual values but not an array, so we unpacked the array by using the es6 (…) spread operator and passed to it.
We can also call Math.min() method without unpacking the array by using the Function.prototype.appl().
Here is an example:
const arr = [2, 4, 1, 55, 34, 12];
const minNumber = Math.min.apply(null, arr);
console.log(maxNumber);