JavaScript Math.min() Method



In JavaScript, the Math.min() method accepts any number of arguments, and it returns the minimum value among those arguments.If any of the arguments is not a number, "NaN" (Not-a-Number) is returned. If there are no arguments, "Infinity" is returned, because there's no minimum value to return.

Note: The Math.min() does not operate on arrays directly as "Math.max()" does. Instead, we need to use it along with the spread syntax (...) to pass an array of values as individual arguments.

Syntax

Following is the syntax of JavaScript Math.min() method −

Math.min(value1, value2, ..., valueN);

Parameters

This method takes in a random number of parameters. The same is described below −

  • value1, value2, ... valueN: These are the numeric values for which we want to find the minimum. We can provide multiple values separated by commas.

Return value

This method returns the largest of the provided numbers.

Example 1

In the following example, we are using the JavaScript Math.min() method to find the minimum among the provided negative numbers −

<html>
<body>
<script>
   let number = Math.min(-5, -15, -25, -55, -100);
   document.write("Lowest number:  ", number);
</script>
</body>
</html>

Output

If we execute the above program, the lowest number will be "-100".

Example 2

Here, we are finding the minimum among the provided postive numbers −

<html>
<body>
<script>
   let number = Math.min(5, 15, 25, 55, 100);
   document.write("lowest number:  ", number);
</script>
</body>
</html>

Output

As we can see the output, the lowest number will be "5".

Example 3

In this example, we are passing the array as an argument to this method −

<html>
<body>
<script>
   let Array = [10, 20, 30, 40, 50, 60, 90];
   let MinNum = Math.min(...Array);
   document.write("Lowest number:  ", MinNum);
</script>
</body>
</html>

Output

The spread operator will destructure the array and then this method finds the lowest number.

Example 4

Here, we are passing string and character arguments to this method −

<html>
<body>
<script>
   let number1 = Math.min("John", 30, 50, 90);
   document.write(number1, "<br>");

   let number2 = Math.min("John", "Smith", "Candice", "Alisa");
   document.write(number2);
</script>
</body>
</html>

Output

If we execute the above program, it returns "NaN" as result.

Example 5

If no arguments are passed to this method, it returns "-Infinity" as result −

<html>
<body>
<script>
   let number = Math.min();
   document.write(number);
</script>
</body>
</html>

Output

As we can see in the output, -Infinity is returned.

Advertisements