Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the minimum value of an array in JavaScript?
JavaScript provides several methods to find the minimum value in an array. The most efficient approach is using Math.min() with the spread operator, though other methods like sorting are also available.
Using Math.min() with Spread Operator (Recommended)
The Math.min() function finds the smallest number among its arguments. Combined with the spread operator (...), it can easily find the minimum value in an array.
<html>
<body>
<button onclick="findMinimum()">Find Minimum</button>
<p id="result"></p>
<script>
function findMinimum() {
var numbers = [6, 39, 55, 1, 44];
var minimum = Math.min(...numbers);
document.getElementById("result").innerHTML =
"Array: [" + numbers + "]<br>Minimum value: " + minimum;
}
</script>
</body>
</html>
Array: [6, 39, 55, 1, 44] Minimum value: 1
Using Array.sort() Method
Sorting the array in ascending order places the minimum value at the first index (index 0).
<html>
<body>
<button onclick="findMinWithSort()">Find Minimum using Sort</button>
<p id="sortResult"></p>
<script>
function findMinWithSort() {
var numbers = [6, 39, 55, 1, 44];
var sortedNumbers = [...numbers].sort(function(a, b) { return a - b; });
var minimum = sortedNumbers[0];
document.getElementById("sortResult").innerHTML =
"Original: [" + numbers + "]<br>" +
"Sorted: [" + sortedNumbers + "]<br>" +
"Minimum: " + minimum;
}
</script>
</body>
</html>
Original: [6, 39, 55, 1, 44] Sorted: [1, 6, 39, 44, 55] Minimum: 1
Using Array.reduce() Method
The reduce() method can iterate through the array and compare each element to find the minimum.
<html>
<body>
<button onclick="findMinWithReduce()">Find Minimum using Reduce</button>
<p id="reduceResult"></p>
<script>
function findMinWithReduce() {
var numbers = [6, 39, 55, 1, 44];
var minimum = numbers.reduce(function(min, current) {
return current < min ? current : min;
});
document.getElementById("reduceResult").innerHTML =
"Array: [" + numbers + "]<br>Minimum: " + minimum;
}
</script>
</body>
</html>
Array: [6, 39, 55, 1, 44] Minimum: 1
Comparison
| Method | Performance | Modifies Original Array | Best For |
|---|---|---|---|
Math.min(...array) |
Fast | No | Small to medium arrays |
array.sort()[0] |
Slower (O(n log n)) | Yes (unless copied) | When you also need sorted array |
array.reduce() |
Good (O(n)) | No | Large arrays, custom logic |
Handling Edge Cases
// Empty array
console.log(Math.min(...[])); // Infinity
// Array with non-numbers
var mixedArray = [5, "hello", 2, null, 8];
console.log(Math.min(...mixedArray)); // NaN
// Proper filtering for mixed arrays
var numbersOnly = mixedArray.filter(function(item) {
return typeof item === 'number' && !isNaN(item);
});
console.log(Math.min(...numbersOnly)); // 2
Infinity NaN 2
Conclusion
For finding minimum values in JavaScript arrays, Math.min(...array) is the most efficient and readable approach. Use reduce() for large arrays or when you need custom logic, and sort() only when you also need the array sorted.
