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:
Math.max()This static-method returns -Infinity, since no arguments are given.
Returns value of type Number.
Math.max(value1)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
value1 | required | The first number to compare. |
This static-method returns the value of value1.
Returns value of type Number.
Math.max(value1, value2)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
value1 | required | The first number to compare. |
value2 | required | The second number to compare. |
This static-method returns the largest of value1 and value2.
Returns value of type Number.
Math.max(value1, value2, /* …, */ valueN)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
value1 | required | The first number to compare. |
value2 | required | The second number to compare. |
valueN | optional | Additional 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,
- We call the
Math.max()method with no arguments. - The result is stored in the variable
result. - We log
resultto the console using theconsole.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,
- We call the
Math.max()method with one argument5. - The result is stored in the variable
result. - We log
resultto the console using theconsole.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,
- We call the
Math.max()method with arguments10,3,15, and7. - The result is stored in the variable
result. - We log
resultto the console using theconsole.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.