Categories
JavaScript Web development

JavaScript max() & min() Methods

JavaScript max() & min() Methods

The JavaScript Math.max() returns the number with the highest value.

Syntax:

Math.max(n1, n2, n3, ..., nX)

n1, n2, n3, …, nX (optional ) – one or more numbers to compare.

Note: A Number, representing the highest number of the arguments, or -Infinity if no arguments are given, or NaN if one or more arguments are not numbers.

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="maxFunction()">Try it</button>
<p id="example-1"></p>

<script>
function maxFunction() {
  document.getElementById("example-1").innerHTML = Math.max(3, 7);
}
</script>

</body>
</html>

Output:

Click the button to return the highest number of 3 and 7:


The JavaScript Math.min() method returns the number with the lowest value.

Syntax:

Math.min(n1, n2, n3, ..., nX)

n1, n2, n3, …, nX (optional ) – one or more numbers to compare.

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="minFunction()">Try it</button>
<p id="example-2"></p>

<script>
function minFunction() {
  document.getElementById("example-2").innerHTML = Math.min(3, 7);
}
</script>

</body>
</html>

Output:

Click the button to return the lowest number of 3 and 7:


Read also:

JavaScript Math Object

JavaScript atan(), atan2() & atanh() Methods

JavaScript Math.fround()