Find Maximum Value of an Array in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Arrays

What you’ll learn

  • The classic linear scan: keep a running maximum and update it whenever you see a larger element.
  • A reusable findMax function and safe handling ideas for empty arrays.
  • A second example with all negative values, plus a live preview and Try it editors.

Overview

You walk the array once. The answer after visiting every slot is the largest value stored, without sorting.

Live preview

Uses the same six integers as example 1: 14, 7, 25, 31, 10, 42.

Live result
Press “Find maximum”.

📜 Pseudocode

Pseudocode
function findMax(arr):   // assume arr.length >= 1
    max = arr[0]
    for i from 1 to arr.length - 1:
        if arr[i] > max:
            max = arr[i]
    return max

Algorithm

Goal: return the largest element from an array of numbers in one pass.

Initialize

Set maxVal = arr[0].

Scan

Loop from index 1 to the end of the array.

Update

If arr[i] > maxVal, assign maxVal = arr[i].

Return

Return maxVal after the loop ends.

1

Find maximum (reference program)

JavaScript
function findMax(arr) {
  let maxVal = arr[0];

  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > maxVal) {
      maxVal = arr[i];
    }
  }

  return maxVal;
}

const array = [14, 7, 25, 31, 10, 42];
const maxValue = findMax(array);

console.log("Maximum value in the array: " + maxValue);
Try it Yourself
2

When every element is negative

JavaScript
function findMax(arr) {
  let maxVal = arr[0];

  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > maxVal) {
      maxVal = arr[i];
    }
  }

  return maxVal;
}

const negatives = [-9, -3, -1, -7];
console.log("Maximum (least negative): " + findMax(negatives));
Try it Yourself

Notes

Parallel idea. Split large arrays into chunks, find each chunk max, then take max of partial results.

Data type. The same algorithm works for integers or floats; only comparison behavior differs for special values like NaN.

❓ FAQ

It picks an initial candidate from the data so every later comparison has something to beat. After one pass, max holds the largest value seen.
This tutorial assumes length >= 1. With zero elements there is no maximum; return null/throw an error or validate before calling findMax.
Yes. You still initialize max to the first element; comparisons use > so the largest value wins even if every entry is negative.
The scan returns the maximum value. Duplicates do not change the answer.
Sorting is more expensive than O(n) for comparison sorts. A linear scan is enough for max alone.
One pass over n elements: O(n) time and O(1) auxiliary space aside from the input array.

🔄 Input / output

These examples use hard-coded arrays. In real tasks, read values from form inputs, API responses, or files, then pass them to findMax.

Edge cases

Empty

arr.length === 0

No maximum exists; guard before reading arr[0].

One element

arr.length === 1

The loop body does not run; the single element is the maximum.

⏱️ Time and space complexity

ApproachTimeExtra space
Single scanO(n)O(1)

Summary

  • Algorithm: running maximum from left to right.
  • Cost: O(n) time, O(1) extra space.
  • Edge: define behavior for empty arrays before calling findMax.
Did you know?

Finding the maximum in an unsorted array needs at least n − 1 comparisons in the worst case. A single left-to-right scan achieves O(n) time and O(1) extra space.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful