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
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value.
Discussions

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
How to use math.max with array
I've found Mozilla Developer Network (MDN) to be a great resource for JavaScript documentation. In this particular case, the last of the three examples they give right at the top of the page for Math.max() is exactly what you're looking for. As a PSA, when looking for JS tips, skip w3schools entirely. You're welcome. More on reddit.com
๐ŸŒ r/Bitburner
11
7
October 21, 2021
Understanding Math.max() - javascript
Math.max doesn't accept an array, just multiple arguments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
functions - What does max[] mean? - Mathematics Stack Exchange
J. M. ain't a mathematician ยท 77k88 gold badges223223 silver badges348348 bronze badges ... Taking the maximal number amongst the parameters. More on math.stackexchange.com
๐ŸŒ math.stackexchange.com
December 6, 2010
๐ŸŒ
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.
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

๐ŸŒ
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java math โ€บ java math max() method
Java Math.max() method with Examples
December 5, 2024 - Letโ€™s use each one of these methods in our examples. Finding the Maximum value of two integers. public class Main { public static void main(String args[]) { int x = 40; int y = 60; System.out.println(Math.max(x, y)); } } The output will be 60.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ guide
JavaScript Math.max() Explained with Examples - Guide
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 .
๐ŸŒ
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)); ...