JavaScript Math min()

The min() method finds the minimum value among the specified values and returns it.

Example

let numbers = Math.min(12, 4, 5, 9, 0, -3);
console.log(numbers); 

// Output:  -3

min() Syntax

The syntax of the min() method is:

Math.min(number1, number2,....)

Here, min() is a static method. Hence, we are accessing the method using the class name, Math.


min() Parameters

The min() method takes in a random number of parameters:

  • number1/number2/… - values among which the minimum number is to be computed

min() Return Value

The min() method returns:

  • the smallest value among the given numbers
  • NaN (Not a Number) for non-numeric arguments

Example 1: JavaScript Math.min()

// min() with negative numbers let numbers1 = Math.min(-1, -11, -132)
; console.log(numbers1);
// min() with positive numbers let numbers2 = Math.min(0.456, 135, 500);
console.log(numbers2); // Output: // -132 // 0.456

In the above example, we have used Math.min() to find the minimum number among:

  • Math.min(-1,-11,-132) - returns -132
  • Math.min(0.456,135,500) - returns 0.456

Example 2: Math.min() with Arrays

let numbers = [4, 1, 2, 55, 9];

// min() with a spread operator let minNum = Math.min(...numbers);
console.log(minNum); // Output: 1

In the above example, we have created an array named numbers. Notice that we are passing the array as an argument to the Math.min() method.

let minNum = Math.min(...numbers);

Here, ... is the spread operator that destructures the array and passes the array values as arguments to min().

The method then finds the smallest number.


Example 3: Math.min() with Non-Numeric Argument

// min() with string argument let numbers1 = Math.min("Dwayne", 2, 5, 79);
console.log(numbers1);
// min() with characters arguments let minNum = Math.min('A', 'B', 'C');
console.log(minNum); // Output: // NaN // NaN

In the above example, we have used the min() method with the string and character arguments. For both arguments, we get NaN as output.


Also Read:

Did you find this article helpful?