To find the smallest value from numbers with JavaScript, we use JavaScript Math.min()
. The Math.min()
is a static function, if everything is fine it will return the smallest value from the numbers. If you didn’t pass any parameters, it will return Infinity
. If you pass any of the parameter is not a number or cannot be converted to a number, it will return NaN
.
Return Value
- Smallest value : If everything is fine, it will return the smallest value from the numbers.
NaN
: If any of the parameter is not a number or cannot be converted to a number, it will returnNaN
.Infinity
: If none parameters.
Example
Return smallest value from numbers:
Math.min(1, 5, 15, -15); // -15
Find the smallest value from array.
?♂️?♂️?♂️
Math.min([1, 3, 6]); // NaN
???
const myArray = [1, 3, 6];
Math.min(...myArray); // 1
If no parameter, it will return infinity
.
Math.min(); // Infinity
Syntax
Math.min(number1, number2, ..., numberX)
Parameters
n1, n2, ..., nX
– Passing one or more numbers to compare and return the smallest value.
More Example
Math.min(32, 10, 213); // 10
Math.min(0, -5, 3, -51, 390, 294); // -51
Math.min(-10, -100, 2, -800 ); // -800
Math.min(5, 15, -3.23); // -3.23
Math.min(-1.5, -2.3, -1.6); // -2.3
const myArray1 = [1, 3, 6];
const myArray2 = [50, -33, 6.52];
Math.min(...myArray1, -5, 2, ...myArray2, 3.21 ); // -33
Browser Support
Browser | Chrome | Edge | Mozilla | Safari | Opera |
Math.min() | Yes | Yes | Yes | Yes | Yes |