JavaScript Math max() method

The max method in JavaScript's Math object evaluates the provided numbers and returns the largest value among them.

Syntax

The syntax for the max method is expressed as follows:

Example

Math.max(num1,num2,....,numN)

Parameter

num1,num2,....,numN - Numbers to be compared..

Return

A maximum value of the given numbers.

JavaScript Math max method example

In this section, we will explore the max function by examining several examples.

Example 1

Let's examine a straightforward example that demonstrates how to display the highest value among a set of provided numbers.

Example

<script>

document.writeln(Math.max(22,34,12,15)+"<br>"); 

document.writeln(Math.max(-10,-24,-12,-20));

</script>

Output:

Output

34

-10

Example 2

Consider the following example that demonstrates how to output the highest value from the provided array.

Example

<script>

var arr=[13,15,12,9]

document.writeln(Math.max(...arr)) 

</script>

Output:

Input Required

This code uses input(). Please provide values below: