Math min() JavaScript Function
Overview
There will be multiple instances when we have to find out the minimum of two or more numbers (any type).
In Javascript programming, we can use the inbuilt Math.min() function to return the smallest number in a set of numbers (there can be to infinitely many numbers in the set).
Syntax of Math min() JavaScript Function
Syntax of Math.min() function in Javascript is:
Here, min() is a static method that is accessible using the Math class.
We can pass as many values as we want to the Math.min() function (even zero).
Parameters of Math min() JavaScript Function
The Math.min() function can take any number of values and it will treat them as a set of numbers.
Return value of JavaScript Math min() function
The Math.min() function returns the smallest value of all the passed values.
In case no value is passed to the function it will return positive infinity as it compares the first value with positive infinity and in absence of a first value, the first comparison will be itself.
If one of the arguments is not a number, NaN will be returned by the math min javascript function.
Example of Math.min() in Javascript
Finding minimum value among the passed arguments using the Math.min() function.
JavaScript Code
Output
In the above example, we are finding the minimum value in , , , and using the Math.min() function.
The math min javascript function is returning as the minimum value among other values.
What is JavaScript Math min() Function?
In Javascript, we can find the minimum of a list of values using the Math.min() function. The min() function is a static method of the built-in Math object in Javascript.
If we pass the numbers , , , and to the Math.min() function, then the function will return as it is the minimum value.
Note: JavaScript numbers are always 64-bit floating point numbers.
More Examples
Let us look at some examples to understand the working of the Math.min() function properly.
Example 1 - Minimum of positive values
In this example, we will pass some positive values to the Math.min() function to find the minimum value.
Code
Output
In the above example, we are finding the minimum value in , , , and using the Math.min() function. The function is returning as the minimum value among other values.
Example 2 - Minimum of Positive and Negative numbers
In this example, we will pass positive and negative values to the Math.min() function to find the minimum value.
Code
Output
In the above example, we are finding the minimum value in , , , and using the Math.min() function. The math min javascript function is returning as the minimum value among other values.
Example 3 - Not passing any value to the Math.min() function
In this example, we will not pass any value to the Math.min() function to see what happens.
Code
Output
In the above example, we haven't passed any value to the Math.min() function, the result is as the arguments are compared with infinity and the passed value is returned. In this case, there were no arguments, so was returned.
This is because when we compare any number to infinity, infinity will always be greater than the number. Think of it like being a default argument to the Math.min() function.
Example 4 -
As we know that we can pass as many numbers as we want to the Math.min() function, but there is a catch if, among the numbers, we also pass a value like string or something else (not a number), the Math.min() function will return NaN (Not A Number).
Code
Output
In the above example, we are passing a string along with other numbers to the Math.min() function. There is no way we can compare a string to a number, which is why the Math.min() function will return NaN (Not A Number).
Supported browsers
Checking if the function we are using is supported by all the browsers is a very necessary step for web development, otherwise, websites will break differently on different browsers.
Let us look at the compatibility of the Math.min() function with different browsers:
Desktop Browsers
Browser | Minimum Version |
---|---|
Chrome | 1 |
Edge | 12 |
Mozilla Firefox | 1 |
Opera | 1 |
Safari | 3 |
Mobile Browsers
Browser | Minimum Version |
---|---|
Chrome Android | 18 |
Firefox for Android | 4 |
Opera | 10.1 |
Samsung Internet | 1.0 |
Safari on IOS | 1 |
Webview Android | 4.4 |
Conclusion
- The Javascript Math Min function is used to return the smallest of the values passed to it.
- The min() function is a static method of the in-built Math object.
- The Math.min() function returns if no value is passed to it and NaN (Not a Number) if a non-numerical value is passed to the function among other values.
- Math min javascript function is supported by every mainstream browser.