Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.min

Returns the smallest of the given numbers.

Available since: ES1

Syntax

Math.min(value1, value2, /* … */)

Parameters

ParameterTypeDescription
value1, …numberThe numbers to compare.

Return value

numberThe smallest of the numbers.

Examples

The smallest of several:

console.log(Math.min(4, 1, 9));

Output:

1

The minimum of an array — with spread:

const prices = [19, 7, 42];
 
console.log(Math.min(...prices));

Output:

7

Clamping a number to a range:

const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
 
console.log(clamp(150, 0, 100));
console.log(clamp(-5, 0, 100));

Output:

100
0

Notes

Without arguments Math.min() returns Infinity, so the minimum of an empty array is Infinity too. Check the array for emptiness first.

See also

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.