JavaScript MAX

The JavaScript MAX function is a Math function used to return the Maximum or Largest value from the specified set of expression. The syntax of the MAX Function in JavaScript Programming language is

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

Value1: It can be a number or a valid numerical expression.

  • For Zero arguments, the MAX function will return Negative Infinity
  • If the value argument is a positive integer and Negative integer, JavaScript max function will return the Output.
  • When any of the argument is not a number, MAX function will return NaN.
  • If the argument is Null, MAX function will convert Null value to Zero.

JavaScript MAX Function Example

We are going to find the JavaScript Maximum value from a different set of data types and display the output

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript MAX Function </title>
</head>
<body>
  <h1> JavaScript MAX Function </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

Here, we used the MAX Function on the string value. It will convert the “3” and “19” to integers and return the output.

document.getElementById("Str").innerHTML = Math.max(2, "3", "19", 9);

Next, We tried MAX math Function with “String”. It can’t convert to an integer so that it will return NaN as output

document.getElementById("Str1").innerHTML = Math.max(2, "String");

Here, we used JavaScript Maximum Function with Null value, and the null has converted to Zero.

document.getElementById("Null").innerHTML = Math.max(4, null, 5, 28);

We tried JavaScript max Function without arguments (Zero arguments). As we said earlier, this returns Negative Infinite

document.getElementById("Multi").innerHTML = Math.max();