I realized the answer as I was posting my own question: This is the most succinct way of taking the min of an array x in JavaScript. The first argument is totally arbitrary; I find the 0 confusing because the code intuitively means "Take the min of 0 and x," which is absolutely not the case. Using the Math object makes more sense for human-readability, but the Raphael.js authors are obsessed with minification and 0 is three bytes shorter.

See http://ejohn.org/blog/fast-javascript-maxmin/

For readability's sake, I'd strongly urge people to stop doing this and instead define a function along the lines of

function arrayMin(arr) { return Math.min.apply(Math, arr); };
Answer from Trevor Burnham on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Math โ€บ min
Math.min() - JavaScript - MDN Web Docs
console.log(Math.min(2, 3, 1)); // Expected output: 1 console.log(Math.min(-2, -3, -1)); // Expected output: -3 const array = [2, 3, 1]; console.log(Math.min(...array)); // Expected output: 1 ... Zero or more numbers among which the lowest value will be selected and returned. The smallest of the given numbers. Returns NaN if any of the parameters is or is converted into NaN.
Top answer
1 of 4
39

I realized the answer as I was posting my own question: This is the most succinct way of taking the min of an array x in JavaScript. The first argument is totally arbitrary; I find the 0 confusing because the code intuitively means "Take the min of 0 and x," which is absolutely not the case. Using the Math object makes more sense for human-readability, but the Raphael.js authors are obsessed with minification and 0 is three bytes shorter.

See http://ejohn.org/blog/fast-javascript-maxmin/

For readability's sake, I'd strongly urge people to stop doing this and instead define a function along the lines of

function arrayMin(arr) { return Math.min.apply(Math, arr); };
2 of 4
14

The reason is this:

  • Your input x is an array

  • The signature of Math.min() doesn't take arrays, only comma separated arguments

  • If you were using Function.prototype.call() it would have almost the same signature, except the first argument is the this context, i.e. who's "calling"

    • Example: Math.min.call(context, num1, num2, num3)
  • The context only matters when you refer to this inside the function, e.g. most methods you can call on an array: Array.prototype.<method> would refer to this (the array to the left of the 'dot') inside the method.

  • Function.prototype.apply() is very similar to .call(), only that instead of taking comma-separated arguments, it now takes an array after the context.

    • Function.prototype.call(context, arg1, arg2, arg3)
    • Function.prototype.apply(context, [arg1, arg2, arg3])
  • The 0 or null that you put in as the first argument is just a place shifter.

Discussions

Why don't Math.max and Math.min accept arrays? Why are they n-arity functions?
or ES6+ version Math.min(...myNums) More on reddit.com
๐ŸŒ r/javascript
33
41
February 13, 2017
How do Javascript Math.max and Math.min actually work? - Stack Overflow
I am really curious how these functions actually work? I know there are a lot of questions about how to use these, I already know how to use them, but I couldn't find anywhere how to actually go ab... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Why does this code excert have to use Math.min.apply rather than just Math.min - Stack Overflow
Math.min requires one or more values passed as parameters. Since the date strings have been pushed into an array, passing an array will just use the array as a value, not the dates. Using apply will pass the elements of the array as arguments, it's effectively the same as using spread syntax: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Find the min/max element of an array in JavaScript - Stack Overflow
@RicardoNolde Unfortunately spreading ... that the Math.min/max functions works (Tested on Chrome v91). If that works for you, please share which browser/version you use. 2021-06-22T14:37:05.31Z+00:00 ... Sorry, I should have been more clear. The NaN problem happens because you're passing a straight array. In the browsers I have tested, it always returns NaN; that can be solved by spreading the array. The other issue you've raised -- the maximum call stack size -- still applies, regardless ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Function โ€บ apply
Function.prototype.apply() - JavaScript - MDN Web Docs
July 10, 2025 - // min/max number in an array const numbers = [5, 6, 2, 3, 7]; // using Math.min/Math.max apply let max = Math.max.apply(null, numbers); // This about equal to Math.max(numbers[0], โ€ฆ) // or Math.max(5, 6, โ€ฆ) let min = Math.min.apply(null, numbers); // vs. loop based algorithm max = -Infinity; ...
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_min.asp
JavaScript Math min() Method
The Math.min() method returns the number with the lowest value.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript โ€บ math_min.htm
JavaScript Math.min() Method
In JavaScript, the Math.min() method accepts any number of arguments, and it returns the minimum value among those arguments.If any of the arguments is not a number, "NaN" (Not-a-Number) is returned.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-math-min-method
JavaScript Math min() Method - GeeksforGeeks
July 15, 2024 - The JavaScript Math min( ) Method is used to return the lowest-valued number passed in the method. The Math.min() method returns NaN if any parameter isn't a number and can't be converted into one.
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ tutorial-applying-math-min-method
Applying the Math.min() Method | Reintech media
January 13, 2026 - Finding the minimum value in an array requires spreading the array elements as individual arguments using the spread operator (...): const temperatures = [2, 9, 5, -3, 12, 7]; const coldestTemp = Math.min(...temperatures); console.log(coldestTemp); // Output: -3 ยท The spread operator approach has limitations with extremely large arrays due to JavaScript's call stack size restrictions.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @vladbezden โ€บ how-to-get-min-or-max-of-an-array-in-javascript-1c264ec6e1aa
How to get min or max of an array in JavaScript | by Vlad Bezden | Medium
September 2, 2016 - var nums = [1, 2, 3] Math.min.apply(Math, nums) // 1 Math.max.apply(Math, nums) // 3 Math.min.apply(null, nums) // 1 Math.max.apply(null, nums) // 3 ยท With ES6/ES2016 destructuring assignment it becomes easier ยท The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects into distinct variables.
๐ŸŒ
HTML5 Game Devs
html5gamedevs.com โ€บ html5 game coding โ€บ coding and game design
how to apply Math.min on a array who have a null value inside?
October 30, 2023 - Hi, I would like to know the lowest value on my array. So i make var arr=[0,1,2,3] console.log(Math.min(...arr)) => 0 ok The problem is that my array contains this : var arr=[null,0,2] var min=Math.min(...arr) arr.indexOf(min) => -1 because it take null as lowest value...how to eject the nu...
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ why don't math.max and math.min accept arrays? why are they n-arity functions?
r/javascript on Reddit: Why don't Math.max and Math.min accept arrays? Why are they n-arity functions?
February 13, 2017 -

Edit: I am not asking for a way to use these methods with arrays. What I am asking is why in the first place do they not accept arrays.

However, I will list the solutions mentioned:

  1. Math.max.apply(null, arr)

  2. Math.max(...arr)

  3. arr.reduce((max, current) => (current > max ? current : max), -Infinity);

Top answer
1 of 7
15

Here is the Math.max code in Chrome V8 engine.

function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER(arg1);
    arg2 = TO_NUMBER(arg2);
    if (arg2 > arg1) return arg2;
    if (arg1 > arg2) return arg1;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1;
    }
    // All comparisons failed, one of the arguments must be NaN.
    return NaN;
  }
  var r = -INFINITY;
  for (var i = 0; i < length; i++) {
    var n = %_Arguments(i);
    n = TO_NUMBER(n);
    // Make sure +0 is considered greater than -0.
    if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) {
      r = n;
    }
  }
  return r;
}

Here is the repository.

2 of 7
7

Below is how to implement the functions if Math.min() and Math.max() did not exist.

Functions have an arguments object, which you can iterate through to get its values.

It's important to note that Math.min() with no arguments returns Infinity, and Math.max() with no arguments returns -Infinity.

function min() {
  var result= Infinity;
  for(var i in arguments) {
    if(arguments[i] < result) {
      result = arguments[i];
    }
  }
  return result;
}

function max() {
  var result= -Infinity;
  for(var i in arguments) {
    if(arguments[i] > result) {
      result = arguments[i];
    }
  }
  return result;
}

//Tests
console.log(min(5,3,-2,4,14));       //-2
console.log(Math.min(5,3,-2,4,14));  //-2

console.log(max(5,3,-2,4,14));       //14
console.log(Math.max(5,3,-2,4,14));  //14

console.log(min());                  //Infinity
console.log(Math.min());             //Infinity

console.log(max());                  //-Infinity
console.log(Math.max());             //-Infinity

๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ js โ€บ math_min.php
JavaScript: Math min() function
The second output to the console log returned 20 which is the smallest of the values 60, 40 and 20. The third output to the console log returned -12 which is the smallest of the values -3, -6, -9 and -12. Finally, we'll take a look at how to use the min() function to find the smallest value ...
๐ŸŒ
Dustin John Pfister
dustinpfister.github.io โ€บ 2020 โ€บ 01 โ€บ 22 โ€บ js-math-max-min
Math max and min methods in javaScript | Dustin John Pfister at github pages
November 21, 2021 - The apply method can be called off of the Math.max or min method as it is a function prototype method, and then a null value can be given as the first argument, along with the array of numbers, more on that later. The Math min and max methods can help save me from having to loop over the contents of an array to find the lowest or highest number in a array of numbers.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Math โ€บ min
JavaScript Math min() - Find Minimum Value | Vultr Docs
November 27, 2024 - The variables a, b, and c are evaluated by Math.min(), which returns the smallest value, -1. Expand array elements as individual arguments using the spread syntax.
๐ŸŒ
Math.js
mathjs.org โ€บ docs โ€บ reference โ€บ functions โ€บ min.html
math.js | an extensive math library for JavaScript and Node.js
In case of a multidimensional array, the minimum of the flattened array will be calculated. When dim is provided, the minimum over the selected dimension will be calculated. Parameter dim is zero-based. math.min(a, b, c, ...) math.min(A) math.min(A, dimension)
๐ŸŒ
Quora
quora.com โ€บ What-is-the-time-complexity-for-JavaScript-Math-min
What is the time complexity for JavaScript Math.min()? - Quora
Math.min() runs in O(n) time when given n numeric arguments (including elements if called with apply/spread). For the common forms: Math.min(a, b) or a fixed small number of arguments: O(1). Math.min(...arr) or Math.min.apply(null, arr) with ...
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Find the Min/Max Elements in an Array in JavaScript
The Math.max function uses the apply() method to find the maximum element in a numeric array: Math.min.apply(Math, testArr); Math.max.apply(Math, testArr); Javascript Math.max.apply find max element in array ยท