JavaScript Math.min() Method
-
Syntax of
Math.min()
-
Example 1: Use
Math.min()
Method in JavaScript -
Example 2: Use the
Array.reduce()
WithMath.min()
Method -
Example 3: Use the
Function.prototype.apply()
WithMath.min()
Method -
Example 4: Use the Spread Operator With
Math.min()
Method -
Example 5: Use
Math.min()
Method With Exception
The Math.min()
method finds out the lowest number from the given numbers. We can pass multiple numbers as the parameter.
But in general, it expects at least two numbers as the parameter so that it can compare them and return the lowest one.
Syntax of Math.min()
Math.min(num1, num2, ....., numN)
Parameters
num |
The parameter num refers to a numeric value. |
Return
The Math.min()
method returns the lowest value from the given values as the parameter.
If any of the values passed in the parameter is non-numeric that can be converted as NaN
, this method will return NaN
.
This method will return Infinity
if nothing is passed as the parameter.
Example 1: Use Math.min()
Method in JavaScript
const result1 = Math.min(3, 11, 31, 50, 15)
const result2 = Math.min(-15, -42, -13, -37, -6)
console.log(result1)
console.log(result2)
Output:
3
-42
First, we take some positive numbers and use this method to find the minimum value. This method returns 3
as the lowest number among the given numbers.
Second, we take some negative numbers to find out the minimum value. It returns -42
, which is the lowest number among all given negative numbers.
Example 2: Use the Array.reduce()
With Math.min()
Method
const arr1 = [10, 50, 1, 33, 20]
const arr2 = []
const result1 = arr1.reduce((x, y) => Math.min(x, y), Infinity)
const result2 = arr2.reduce((x, y) => Math.min(x, y), Infinity)
console.log(result1)
console.log(result2)
Output:
1
Infinity
The array.reduce()
method iterates the whole array and applies the Math.min()
method. It compares the numeric values of the array and returns the lowest one.
It will return Infinity
if the array does not contain any value.
Example 3: Use the Function.prototype.apply()
With Math.min()
Method
const arr = [10, 50, 1, 40, 11]
function getMinNumFromArray(arr){
return Math.min.apply(null, arr)
}
console.log(getMinNumFromArray(arr))
Output:
1
The Math.min()
method does not work with an array. The Function.prototype.apply()
is used with Math.min()
to get the lowest number from an array.
Example 4: Use the Spread Operator With Math.min()
Method
const arr = [1, 50, 31, 17, 26]
const result = Math.min(...arr)
console.log(result)
Output:
1
The spread operator ...
converts an array into individual elements. The Math.min()
method finds the lowest number among those elements of an array.
Example 5: Use Math.min()
Method With Exception
const result1 = Math.min(2, 12, 'z')
const result2 = Math.min()
console.log(result1)
console.log(result2)
Output:
NaN
Infinity
First, we pass some numbers and a string as the parameter of this method. This method returns NaN
as it cannot deal with non-numeric numbers that can be converted into NaN
.
Second, we pass nothing in the parameter of this method, and it returns Infinity
.
Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.
LinkedIn