Math min() JavaScript Function

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

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 00 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 8383, 00, 2121, and 1000010000 using the Math.min() function.
The math min javascript function is returning 00 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 22, 55, 99, and 11 to the Math.min() function, then the function will return 11 as it is the minimum value.

MATH MIN PYTHON

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 22, 44, 99, and 1212 using the Math.min() function. The function is returning 22 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 22, 4-4, 00, and 34-34 using the Math.min() function. The math min javascript function is returning 34-34 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 InfinityInfinity as the arguments are compared with infinity and the passed value is returned. In this case, there were no arguments, so InfinityInfinity was returned.
This is because when we compare any number to infinity, infinity will always be greater than the number. Think of it like InfinityInfinity 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

BrowserMinimum Version
Chrome1
Edge12
Mozilla Firefox1
Opera1
Safari3

Mobile Browsers

BrowserMinimum Version
Chrome Android18
Firefox for Android4
Opera10.1
Samsung Internet1.0
Safari on IOS1
Webview Android4.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 InfinityInfinity 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.