offline version v3


39 of 264 menu

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

Math.max(number, number, number...);

Example

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

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

The code execution result:

100

Example

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

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

The code execution result:

0

Example

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

console.log(Math.max());

The code execution result:

-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:

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

The code execution result:

100

Example

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

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

The code execution result:

100

See also

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