Open In App

JavaScript – Min Element in an Array

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

These are the following ways to find the minimum element in JS array:

Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the minimum element among them.

1. Using Math.min() Method (Efficient for Small Arrays)

This method uses the Math.min() function to find the smallest value in the array by spreading the elements as individual arguments. It’s simple and concise but can be less efficient for large arrays due to the spread operator's overhead.

Note: The min() function returns “Infinity” for the empty array.

JavaScript
const a = [5, 12, 8, 130, 44];
const res = Math.min(...a); 
console.log(res);  

Output
5

2. Using for Loop (Simple & Efficient)

The for loop iterates through the array, updating the smallest value (res) whenever a smaller element is found. This approach is efficient, with O(n) time complexity, and is ideal for performance-sensitive situations.

JavaScript
const a = [5, 12, 8, 130, 44];
let res = a[0];  
for (let i = 1; i < a.length; i++) {
    if (a[i] < res) {
        res = a[i];  
    }
}
console.log(res);  

Output
5

3. Using reduce() Method (Efficient for Large Arrays)

The reduce() method processes each element of the array, comparing it with an accumulator (res) to track the smallest value. It’s functional and concise, with O(n) time complexity, and is great for functional programming enthusiasts.

JavaScript
const a = [5, 12, 8, 130, 44];
const res = a.reduce((min, c) => (c < min ? c : min), a[0]);
console.log(res); 

Output
5

4. Using forEach() Method (Simple but slightly less efficient)

The forEach() method iterates over the array and updates res if a smaller element is found. While simple, it’s slightly less efficient than the for loop due to the overhead of invoking a callback function. Time complexity: O(n).

JavaScript
const a = [5, 12, 8, 130, 44];
let res = a[0];  
a.forEach(e => {
    if (e < res) {
        res = e; 
    }
});
console.log(res);

Output
5

5. Using sort() Method (Not Efficient)

This approach sorts the array in ascending order and picks the first element. It’s simple but not efficient for this task, as sorting has a time complexity of O(n log n), which is slower than simply iterating through the array.

JavaScript
const a = [5, 12, 8, 130, 44];
a.sort((x, y) => x - y);  
const res = a[0];  
console.log(res);  

Output
5

6. Using Math.min.apply() (Efficient for Any Array)

This method is similar to using the spread operator but uses apply() to pass the array as arguments to Math.min(). It’s less readable but still efficient, with O(n) time complexity, and is useful in environments that don’t support the spread operator.

JavaScript
const a = [5, 12, 8, 130, 44];
const res = Math.min.apply(null, a);
console.log(res);  

Output
5

Next Article

Similar Reads