Array Max & Min
Program to find maximum and minimum element in an array
JavaScript Array Max & Min Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Using Math.max and Math.min with spread operator
let numbers = [10, 5, 20, 15, 8, 25];
let max = Math.max(...numbers);
let min = Math.min(...numbers);
console.log("Array:", numbers);
console.log("Maximum:", max);
console.log("Minimum:", min);
// Method 2: Using for loop
function findMaxMin(arr) {
if (arr.length === 0) return { max: null, min: null };
let max = arr[0];
let min = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
return { max, min };
}
let result = findMaxMin([10, 5, 20, 15, 8]);
console.log("\nUsing loop:", result);
// Method 3: Using reduce
function findMaxMinReduce(arr) {
return arr.reduce((acc, num) => {
acc.max = Math.max(acc.max, num);
acc.min = Math.min(acc.min, num);
return acc;
}, { max: arr[0], min: arr[0] });
}
console.log("\nUsing reduce:", findMaxMinReduce([3, 7, 2, 9, 1]));
// Method 4: Using sort
function findMaxMinSort(arr) {
let sorted = [...arr].sort((a, b) => a - b);
return {
min: sorted[0],
max: sorted[sorted.length - 1]
};
}
console.log("\nUsing sort:", findMaxMinSort([12, 4, 8, 19, 3]));Array: [ 10, 5, 20, 15, 8, 25 ]
Maximum: 25
Minimum: 5
Using loop: { max: 20, min: 5 }
Using reduce: { max: 9, min: 1 }
Using sort: { min: 3, max: 19 }Understanding Array Max & Min
This program demonstrates different methods to find maximum and minimum values in an array.
Method 1: Math.max/min with Spread
Using ES6 spread operator:
let max = Math.max(...numbers);
let min = Math.min(...numbers);
Spread Operator (...):
Math.max(...[1,2,3]) → Math.max(1,2,3)Method 2: For Loop
Iterative approach:
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
Pros:
Method 3: Reduce
Functional approach:
arr.reduce((acc, num) => {
acc.max = Math.max(acc.max, num);
acc.min = Math.min(acc.min, num);
}, { max: arr[0], min: arr[0] });
return acc;Reduce Method:
Method 4: Sort
Sort array and get first/last:
let sorted = [...arr].sort((a, b) => a - b);
return { min: sorted[0], max: sorted[sorted.length - 1] };Note:
Creates copy with spread to avoid mutating original
Time Complexity:
When to Use:
-
Spread/Math
: Simplest, modern
-
Loop
: Learning, custom logic
-
Reduce
: Functional style
-
Sort
: Avoid for this use case
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Array Max & Min
This JavaScript program is part of the "Array Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.