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.

Answer from Charlie on Stack Overflow
🌐
WebPlatform
webplatform.github.io › docs › javascript › Math › max
max · WebPlatform Docs
javascript · Math · max · Returns the larger of a set of supplied numeric expressions. Math.max([ number1 [, number2 [... [, numberN ]]]]) The following code shows how to get the larger of two expressions. var x = Math.max(107 - 3, 48 * 90); document.write(x); // Output: // 4320 ·
🌐
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 - In this section I will be going over a few basic examples of the Math.min, and Math max methods in the Math object of core javaScript. I will also be touching base on any and all other related topics in this basic section before getting into more advanced topics and any and all simple project examples.
🌐
GitHub
github.com › josdejong › mathjs › issues › 212
Improve performance of `max` and `min` · Issue #212 · josdejong/mathjs
Using Math.max.apply(null, array) is ~100x faster on large arrays than the current max and min implementation. Note that Chrome has a limit of ~100k arguments when using .apply so arrays larger than 100k values need to be done in batches...
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

Find elsewhere
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Math › max
Math.max() - JavaScript
The Math.max() function returns the largest of the zero or more numbers given as input parameters. The source for this interactive example is stored in a GitHub repository.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › min
Math.min() - JavaScript | MDN
Math.max() can be used in a similar way to clip a value at the other end. ... This page was last modified on Jul 20, 2025 by MDN contributors. View this page on GitHub • Report a problem with this content
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-math-max-method
JavaScript Math max() Method | GeeksforGeeks
September 30, 2024 - So, the natural log of 10 is represented as ln(10) whose value is approximately 2.302Syntax:Math.LN10; Return Values: It simply returns the ... The Javascript Math.LOG2E is a property in JavaScript that is simply used to find the value of base 2 logarithms of e, where e is an irrational and transcendental number approximately equal to 1.442.Syntax:Math.LOG2E;Return Values: It simply returns the value of the base 2 logarithms of e.Example: H
🌐
GitHub
github.com › MikeMcl › big.js › issues › 86
Implement Math.min / Math.max equivalents · Issue #86 · MikeMcl/big.js
June 18, 2017 - Can I have Big.min and Big.max implementations, e.g. as statics on the constructor? const a = new Big(12); const b = new Big(42); const c = new Big("0.1"); assert(Big.max(a, b, c).eq(b)); assert(Big.min(a, b, c).eq(c));
Published   Sep 20, 2017
🌐
MDN
mdn2.netlify.app › en-us › docs › web › javascript › reference › global_objects › math › max
Math.max() - JavaScript | MDN
var arr = [1, 2, 3]; var max = Math.max(...arr); However, both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See Using apply and built-in functions for more details. The reduce solution does not have this problem. BCD tables only load in the browser · Math.min() Edit on GitHub ·
🌐
GitHub
gist.github.com › leodutra › 63ca94fe86dcffee1bab
Fast Int Math + Bitwise Hacks For JavaScript · GitHub
Fast Int Math + Bitwise Hacks For JavaScript · Raw · bitwise-hacks.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters · Show hidden characters · Copy link · May be var MIN_SIGNED_32_BIT_INT = ~MAX_SIGNED_32_BIT_INT + 1 ?
🌐
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value.
🌐
TechOnTheNet
techonthenet.com › js › math_max.php
JavaScript: Math max() function
In JavaScript, max() is a function that is used to return the largest value from the numbers provided as parameters. Because the max() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
🌐
GitHub
gist.github.com › Kalkwst › a7854761fe0e4ebfbc5f5f74313f0ee2
Math.max-implementation.js · GitHub
Math.max-implementation.js · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
GitHub
gist.github.com › engelen › fbce4476c9e68c52ff7e5c2da5c24a28
Single-line ArgMax for JavaScript · GitHub
/** * Find List of ArgMax, empty if list is empty * @param array iterable * @returns all indices where the Array has the maximal value */ export function argMax(array): number[] { return [].reduce.call( array, (aMax: number[], current, idx: number, arr: any[]) => { // for idx=0, aMax is empty, arr[0] === current, 0 is pushed, then never empty again const max = arr[aMax[0] || 0]; if (current > max) return [idx]; if (max === current) aMax.push(idx); return aMax; }, [] ); }
🌐
DEV Community
dev.to › igadii › why-mathmin-mathmax-in-javascript-makes-complete-sense-as-it-is-164p
Why is Math.min() Greater-Than Math.max() in JavaScript - DEV Community
March 11, 2025 - In the examples, we saw that min() and max() methods are basically comparing the values present in the set and returning the smallest or the largest value respectively. Now, what if we pass a set with only a single value. /* Math.min() */ console.log(Math.min(7)); // Output: 7 /* Math.max() */ console.log(Math.max(7)); // Output: 7