JavaScript max

The JavaScript max function is a math function that returns the Maximum or Largest value from the specified set of expressions. The syntax of the max Function is

Math.max(Value1, Value2, ...ValueN);

Parameters (Value 1 to Value N): It can be two or more numbers or a valid numerical expression to find the maximum value.

Return Value

  • For zero arguments, the JS max() function will return a Negative Infinity
  • If the value argument is a positive or a Negative integer, it will return the Output.
  • When any argument is not a number, it will return NaN.
  • If the argument is Null, it will convert the Null value to zero.

JavaScript max Function Example

We will find the Maximum value from a different set of data types and display the output. For the Str1 element id, we used the max Function on the string to find the maximum value. It will convert the “3” and “19” to integers and return the output.

Next, we tried this math method with “String”. It can’t convert to an integer, so it will return NaN as output. Next, we used the Maximum Function with a Null value, and the null was converted to Zero.

Within the last line, we tried the JavaScript max Function without arguments (Zero arguments). As we said earlier, JavaScript returns Negative Infinity.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptMAXFunction </title>
</head>
<body>
  <h1> JavaScriptMAXFunction </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Str1"></p>
  <p id = "Null"></p>
  <p id = "Multi"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.max(10, 20, 3);
    document.getElementById("Neg").innerHTML = Math.max(-1, 2, -9, -7);
    document.getElementById("Dec").innerHTML = Math.max(12.50, 14.05, 9.35, 4.58);
    document.getElementById("Neg_Dec").innerHTML = Math.max(-3.10,-6.05, -22.04, -15.08);
    document.getElementById("Str").innerHTML = Math.max(2, "3", "19", 9);
    document.getElementById("Str1").innerHTML = Math.max(2, "String");
    document.getElementById("Null").innerHTML = Math.max(4, null, 5, 28);
    document.getElementById("Multi").innerHTML = Math.max();
  </script>
</body>
</html>
JavaScript max Function Example

JavaScript max Date in an array

We can use the Math.max() function with the spread operator to find the maximum date in an array of elements (dates). However, we must use the new Date() object to display the result in the date format. Otherwise, it returns the result in milliseconds.

const dates = [
new Date('2026-01-25'),
new Date('2026-06-10'),
new Date('2026-04-18'),
new Date('2026-06-20'),
new Date('2026-11-30')
];

const maximumDate = new Date(Math.max(...dates));
console.log(maximumDate);

const mDate = Math.max(...dates);
console.log(mDate);
Mon Nov 30 2026 05:30:00 GMT+0530 (India Standard Time) 
1795996800000

Similar to the above, we can use the combination of mah.max() and the spread operator to find the maximum value in an array of integers.

const a = [19, 20, 8, 40, 70, 66, 50];
const mx = Math.max(...a);
console.log(mx);
70

Alternatively, we can use the reduce() method with an arrow function to apply the max() function to the array elements.

const a = [19, 20, 8, 40, 70, 66, 50];
const mx = a.reduce((a, b) => Math.max(a, b));
console.log(mx);
70

JavaScript find max value in an array of objects

Similar to the regular array, we use the Math.max() function to find the maximum value in an array of objects. For this, we use the spread operator to apply to an array and then utilize the map() function to extract the required array object that we want to compare to find the maximum value.

For employee maximum age, use against the age object, and for the highest salary, apply the max() function to Salary.

const employees = [
{ name: "Tracy", age: 25, Salary: 140000 },
{ name: "John", age: 39, Salary: 100000 },
{ name: "Charlie", age: 35, Salary: 120000 }
];

const mxAge = Math.max(...employees.map(emp => emp.age));
console.log(mxAge);

const mxSal = Math.max(...employees.map(emp => emp.Salary));
console.log(mxSal);
39 
140000

How does Math.max() handle null, Undefined, and NaN?

The JavaScript Math.max() function returns the following results if any one of the arguments is null, undefined, or NaN.

  • It treats null as 0, so it returns the maximum value from the given array as the output.
  • If it is undefined or NaN (not a number), the max() function returns NaN as the output.

If null, undefined, or NaN values are inside an array, the max() function result will be the same. The following program shows the Math.max() function results.

console.log(Math.max(null));
console.log(Math.max(14, 17, null, 7));

console.log(Math.max(undefined));
console.log(Math.max(5, undefined, 33, 40));

console.log(Math.max(NaN));
console.log(Math.max(19, NaN, 40));
0 
17 
NaN 
NaN 
NaN 
NaN

Related Math functions