JavaScript is a popular programming language that is widely used for web development, and it includes a variety of built-in functions and methods that make it easier to perform mathematical operations. One of these functions is the Math.min()
method, which is used to determine the smallest value in a set of numbers.
The Math.min()
method takes one or more arguments, which can be numbers or variables that contain numbers. It then returns the smallest value among those arguments. This function is useful in a variety of scenarios, such as finding the smallest value in an array or comparing the values of multiple variables.
Sorting Numbers in JavaScript
Before we dive into the Math.min()
method, it's worth mentioning that JavaScript provides several ways to sort numbers. The most straightforward way is to use the sort()
method of the Array
object, which sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
However, sort()
can be tricky when sorting numbers. For example, if we sort the array [5, 10, 2, 8, 1]
, using the sort()
method, we get the following array: [1, 10, 2, 5, 8]
.
const arr = [5, 10, 2, 8, 1];
arr.sort();
console.log(arr);
Output
[ 1, 10, 2, 5, 8 ]
This happens because sort()
sorts the numbers lexicographically as strings. To sort the array numerically, we need to pass a comparison function to sort()
as an argument. The comparison function takes two arguments and should return a negative, zero, or positive number, depending on whether the first argument is less than, equal to, or greater than the second argument. The following code shows how to sort the same array numerically and select the smallest number within the array.
const arr = [5, 10, 2, 8, 1];
arr.sort((a, b) => a - b);
console.log(arr);
const min = arr[0];
console.log(min);
Output
[ 1, 2, 5, 8, 10 ]
1
But instead of going through this process, we can make use of the Math.min()
method.
Using the Math.min()
Method in JavaScript
Now let's explore the Math.min()
method in detail. As mentioned earlier, Math.min()
returns the smallest of zero or more numbers passed to it as arguments. If no arguments are provided, it returns Infinity
. The method works with both positive and negative numbers, as well as with non-number values such as NaN
and undefined
. Let's look at the syntax of the Math.min()
method.
The syntax of the Math.min()
method can be seen below
Math.min(value1, value2, value3, ..., valueN)
The Math.min()
method takes zero or more arguments, separated by commas. The square brackets indicate that the arguments are optional. The method returns the smallest of the arguments passed to it.
Example 1:Â Finding the minimum value in an array
Let's look at a simple example to understand the usage of Math.min()
. In the following code, we pass several numbers to the Math.min()
method as arguments and store the result in a variable.
const a = 6;
const b = 12;
const c = 18;
const d = 22;
const smallest = Math.min(a, b, c, d);
console.log(smallest);
Output
6
In this example, we pass four numbers as arguments to the Math.min()
method, and it returns the smallest number, which is 6
.
Example 2: Finding smallest value among negative numbers
Let's look at another example to understand how Math.min()
works with negative numbers. In the following code, we pass two negative numbers and one positive number to the Math.min()
method as arguments.
const a = -13;
const b = -5;
const c = 17;
const d = 19;
const smallest = Math.min(a, b, c, d);
console.log(smallest);
Output
-13
In this example, we passed two negative numbers and one positive number as arguments to the Math.min()
method. The method correctly returns the smallest number, which is -13
.
Before, we go to another example, it makes no sense to have 100 values stored within variables to compare to use with the Math.min()
method. Typically, our numerical data will be stored within an array-like structure, and we can make use of the spread operator to pass the array to the Math.min()
method. To illustrate this, we store the values we previously used
const a = -13;
const b = -5;
const c = 17;
const d = 19;
const tempArr = [a, b, c, d];
const smallest = Math.min(...tempArr);
console.log(smallest);
Output
-13
Example 3: Working with non-number values
The Math.min()
method also works with non-number values such as NaN
and undefined
. In the following code, we pass a mixture of number and non-number values to the Math.min()
method as arguments.
const a = 10;
const b = NaN;
const c = 15;
const d = undefined;
const smallest = Math.min(a, b, c, d);
console.log(smallest);
Output
NaN
In this example, we pass a number (a
), NaN
(b
), undefined
(d
), and another number (c
) to the Math.min()
method. The method returns NaN
, which is the smallest value among the arguments passed to it. Note that if any argument passed to the Math.min()
method is NaN
, the method always returns NaN
.
Example 4: Finding the Second Smallest Number in an Array
Suppose we have an array of numbers and we want to find the second smallest number in the array. We can use the Math.min()
method in conjunction with the Array.prototype.filter()
method to achieve this.
const arr = [12, 8, 2, 18, 1123];
const smallest = Math.min(...arr);
const secondSmallest = Math.min(...arr.filter((x) => x !== smallest));
console.log(secondSmallest);
Output
8
In this example, we first find the smallest number in the array (2
) using the Math.min()
method. We then filter out that number from the array using the Array.prototype.filter()
method, which returns a new array with all elements except for the smallest number. We then apply the Math.min()
method on the filtered array to find the second smallest number (8
).
Example 5: Comparing multiple variables
Suppose we have multiple variables containing numbers, and we want to find the smallest of them. We can use Math.min()
to accomplish this task as shown below:
let num1 = 5;
let num2 = 10;
let num3 = 15;
let smallestNumber = Math.min(num1, num2, num3);
console.log(smallestNumber); // Output: 5
In this example, we pass the three variables containing numbers as arguments to Math.min()
. The function then returns the smallest number among them, which we store in the smallestNumber
variable and print to the console.
Summary
In this article, we explored the Math.min()
method in JavaScript. We learned that it returns the smallest of zero or more numbers passed to it as arguments. We also looked at the syntax of the method and several examples to understand how it works. We also saw that Math.min()
works with non-number values such as NaN
and undefined
. Finally, we saw how to use Math.min()
with an array of numbers using the spread operator.
References
Math.min() - JavaScript | MDN (mozilla.org)