Math.max method

The Math.max method returns the largest number from the group of numbers passed to the function. If nothing is passed to the function, then -Infinity will be returned.

Syntax

javascriptMath.max(number, number, number...);

Example

Let's output the maximum number from a group of numbers:

javascriptconsole.log(Math.max(1, 5, 10, 34, 100));

The code execution result:

javascript100

Example

Let's output the maximum number from a group of numbers:

javascriptconsole.log(Math.max(-1, 0, -20, -56, -100));

The code execution result:

javascript0

Example

The following example will output -Infinity because no parameters were passed to the method:

javascriptconsole.log(Math.max());

The code execution result:

javascript-Infinity

Example

By default, the function does not work with arrays. However, it can be made to do so with the spread operator. Let's use it to display the largest value of the array:

javascriptlet arr = [1, 5, 10, 34, 100];
let max = Math.max(...arr);

console.log(max);

The code execution result:

javascript100

Example

You can also make the function work with arrays using the apply method:

javascriptlet arr = [1, 5, 10, 34, 100];
let max = Math.max.apply(null, arr);

console.log(max);

The code execution result:

javascript100

See also

  • the Math.min method
    that returns the minimum number from a group of numbers