Math.min()

Returns the smallest of the numbers passed to it, or Infinity when given none.

Signature
Math.min(value1, value2, /* …, */ valueN)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$value1, …, valueNoptionalnumberZero or more numbers among which the lowest is selected. Non-numbers are coerced first.

Return value

numberThe smallest of the given numbers. Returns NaN if any argument is or converts to NaN, and Infinity if called with no arguments.

Errors

Nothing on its own, but spreading a very large array into it can exceed the engine's argument limit and throw a RangeError.

Math.min() takes any number of arguments and returns the smallest. Like its counterpart it does not accept an array, so you spread: Math.min(...arr).

When to reach for it

Besides finding a minimum, its most common role is capping an upper bound. Math.min(100, percent) stops a progress value overshooting, and Math.min(available, requested) is the standard "take as much as you can" pattern. Combined with Math.max() it forms the usual clamp.

What trips people up

The empty case returns Infinity, which is the identity for minimum. Math.min(...[]) is Infinity, and if that lands in a UI it reads as a bug rather than as "no data". Always guard the empty array.

Any argument that coerces to NaN makes the whole result NaN. Because coercion is silent, undefined from a missing object property is the usual culprit — Math.min(...items.map(i => i.price)) returns NaN the moment one item lacks a price.

Also note that Math.min() distinguishes -0 from 0: Math.min(0, -0) returns -0. And spreading arrays of six figures or more risks a RangeError from the engine's argument limit; use reduce there.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Capping a progress or percentage value at 100
  • Taking the smaller of requested and available quantity
  • Finding the cheapest option in a small list of prices
  • Clamping a value into a range together with Math.max()

Frequently asked questions

How do I find the minimum value of an array in JavaScript?

Use Math.min(...array) to spread the elements into arguments. For very large arrays use array.reduce((a, b) => b < a ? b : a, Infinity), since spread can hit the engine's argument-count limit.

Why does Math.min() return Infinity?

Because it was called with no arguments, typically from spreading an empty array. Infinity is the identity for minimum, so any real number compared against it wins.

How do I clamp a number between two values?

Use Math.min(hi, Math.max(lo, value)). Math.max pulls the value up to the lower bound and Math.min pushes it down to the upper bound.

More examples

A missing property silently yields NaN

The third item has no price, so map produces undefined, which coerces to NaN and poisons the result. Filtering to finite numbers fixes it. The last lines show the guard needed for empty input, since Math.min(...[]) would return Infinity.

Example · javascript
const items = [{ price: 8 }, { price: 3 }, { name: "gift" }];

console.log(Math.min(...items.map((i) => i.price))); // NaN

const cheapest = Math.min(
  ...items.map((i) => i.price).filter(Number.isFinite)
);
console.log(cheapest); // 3

const empty = [];
console.log(empty.length ? Math.min(...empty) : null); // null

Clamp and reduce

The clamp maps out-of-range values onto the bounds and leaves in-range values alone. The reduce finds the minimum without spreading 200,000 arguments onto the call stack, which is the approach to use once an array grows past a few tens of thousands of elements.

Example · javascript
const clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, n));
console.log([-20, 0.5, 250].map((n) => clamp(n, 0, 100)));

// Safe for arrays of any size
const big = Array.from({ length: 200000 }, (_, i) => (i * 13) % 7919);
console.log(big.reduce((a, b) => (b < a ? b : a), Infinity));

Discussion

  • Be the first to comment on this lesson.