JavaScript Math.max()
Syntax & Examples


Math.max() static-method

The Math.max() static method in JavaScript returns the largest of zero or more numbers. If no arguments are given, the result is -Infinity.


Syntax of Math.max()

There are 4 variations for the syntax of Math.max() static-method. They are:

1.
Math.max()

This static-method returns -Infinity, since no arguments are given.

Returns value of type Number.

2.
Math.max(value1)

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to compare.

This static-method returns the value of value1.

Returns value of type Number.

3.
Math.max(value1, value2)

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to compare.
value2requiredThe second number to compare.

This static-method returns the largest of value1 and value2.

Returns value of type Number.

4.
Math.max(value1, value2, /* …, */ valueN)

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to compare.
value2requiredThe second number to compare.
valueNoptionalAdditional numbers to compare.

This static-method returns the largest of value1, value2, and additional arguments valueN.

Returns value of type Number.



✐ Examples

1 Using Math.max() with no arguments

In JavaScript, if no arguments are provided to the Math.max() method, it returns -Infinity.

For example,

  1. We call the Math.max() method with no arguments.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.max();
console.log(result);

Output

-Infinity

2 Using Math.max() with one argument

In JavaScript, we can use the Math.max() method to find the largest of one number, which is the number itself.

For example,

  1. We call the Math.max() method with one argument 5.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.max(5);
console.log(result);

Output

5

3 Using Math.max() with multiple arguments

In JavaScript, we can use the Math.max() method to find the largest of multiple numbers.

For example,

  1. We call the Math.max() method with arguments 10, 3, 15, and 7.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.max(10, 3, 15, 7);
console.log(result);

Output

15

Summary

In this JavaScript tutorial, we learned about max() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.