Find Minimum 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 minimum and update it whenever you see a smaller element.
  • A reusable findMin 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 smallest value stored, without sorting.

Live preview

Uses the same seven integers as example 1: 12, 5, 7, 3, 2, 8, 10.

Live result
Press “Find minimum”.

📜 Pseudocode

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

Algorithm

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

Initialize

Set minVal = arr[0].

Scan

Loop from index 1 to the end of the array.

Update

If arr[i] < minVal, assign minVal = arr[i].

Return

Return minVal after the loop ends.

1

Find minimum (reference program)

JavaScript
function findMin(arr) {
  let minVal = arr[0];

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

  return minVal;
}

const array = [12, 5, 7, 3, 2, 8, 10];
const minValue = findMin(array);

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

When every element is negative

JavaScript
function findMin(arr) {
  let minVal = arr[0];

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

  return minVal;
}

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

Notes

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

Data type. The same algorithm works for integers or floats; comparison edge cases can appear with values like NaN.

❓ FAQ

It is the smallest value in the list. For signed numbers, that can be the most negative value.
You need a first candidate from real data. Then every next element can replace that candidate if it is smaller.
There is no minimum for an empty array. Validate length first and return null/throw an error in production code.
Yes. The same comparison arr[i] < minVal correctly picks the most negative value when all numbers are negative.
No. Sorting does more work than necessary. A single scan is enough to get only the minimum.
One pass over n items gives O(n) time and O(1) extra space.

🔄 Input / output

These examples use fixed arrays. In real tasks, read values from form inputs, APIs, or files, then pass them to findMin.

Edge cases

Empty

arr.length === 0

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

One element

arr.length === 1

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

⏱️ Time and space complexity

ApproachTimeExtra space
Single scanO(n)O(1)

Summary

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

Finding the minimum in an unsorted list still needs only one left-to-right pass: 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