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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › max
Math.max() - JavaScript | MDN
The Math.max() static method returns the largest of the numbers given as input parameters, or -Infinity if there are no parameters.
🌐
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value.
🌐
Programiz
programiz.com › javascript › library › math › max
JavaScript Math max()
The max() method finds the maximum value among the specified values and returns it. let numbers = Math.max(12, 4, 5, 9, 0, -3); console.log(numbers); // Output: 12
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

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-max-method
JavaScript Math max() Method - GeeksforGeeks
September 30, 2024 - The Math.max() method in JavaScript returns the largest value from a set of zero or more numbers.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › max
JavaScript Math max() - Find Maximum Value | Vultr Docs
November 27, 2024 - The method identifies 20 as the maximum value. Note that JavaScript treats 0 and -0 as equal, and both are considered less than any positive number. Understand that direct comparisons using Math.max() are typically more performant and concise than manual iterative approaches.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_max_int.htm
Java - Math max(int x, int y) Method
The java.lang.Math.max(int a, int b) returns the greater of two int values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.
🌐
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
System.out.println(Math.max(2.0, ...(Math.max(96L, 2048L)); Try it Yourself » · The max() method returns the number with the highest value from a pair of numbers....
🌐
Wikipedia
en.wikipedia.org › wiki › Maximum_and_minimum
Maximum and minimum - Wikipedia
January 17, 2026 - In mathematical analysis, the maximum and minimum of a function are, respectively, the greatest and least value taken by the function. Known generically as extrema, they may be defined either within a given range (the local or relative extrema) ...
🌐
Math.js
mathjs.org › docs › reference › functions › max.html
math.js
In case of a multidimensional array, the maximum of the flattened array will be calculated. When dim is provided, the maximum over the selected dimension will be calculated. Parameter dim is zero-based. math.max(a, b, c, ...) math.max(A) math.max(A, dimension)
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-max-method
Java Math max() Method - GeeksforGeeks
May 14, 2025 - The max() method in Java is a part of java.lang.Math class. This is an inbuilt function in Java that returns maximum of two numbers.
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › guide
JavaScript Math.max() Explained with Examples
September 8, 2019 - Math Max Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN .
🌐
TutorialsPoint
tutorialspoint.com › javascript › math_max.htm
JavaScript Math.max() Method
The Math.max() method in JavaScript accepts a set of arguments as parameters, finds the highest numeric value among it, and returns it. If no arguments are provided, -Infinity is returned, indicating that there is no maximum value.
🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › appendices › math-method-examples › max-and-min-examples.html
Math.max and Math.min Examples — Introduction to Professional Web Development in JavaScript documentation
Math.max and Math.min Examples · The general syntax for this method is: Math.max(x, y, z, ...) This method finds and returns the largest value from a set of numbers (x, y, z, ...). To find the maximum value in an array, see below. Example · console.log(Math.max(2, 3, 100.01, 0, -5.2, 100)); ...