# JavaScript Math max()

The JavaScript Math.max() function returns the number with the highest value.

The syntax of the `Math.max()` function is:

``Math.max(n1, n2, ..., nx)``

`max()`, being a static method, is called using the `Math` class name.

## Math.max() Parameters

The `Math.max()` function takes in arbitrary (one or more) numbers as arguments.

## Return value from Math.max()

• Returns the largest of the given numbers.
• Returns `NaN` if at least one argument cannot be converted to a number.
• Returns `-Infinity` if no arguments are given. `-Infinity` is the initial comparant as all numbers are greater than `-Infinity`.

## Example 1: Using Math.max()

``````// using Math.max()

var num = Math.max(12, 4, 5, -9, 35);
console.log(num); // 35

var num = Math.max(-0.456, -1);
console.log(num); // -0.456

// Returns -Infinity for no arguments
var num = Math.max();
console.log(num); // -Infinity

// Returns NaN if any non-numeric argument
var num = Math.max("JS", 2, 5, 79);
console.log(num); // NaN``````

Output

```35
-0.456
-Infinity
NaN```

## Example 2: Using Math.max() on Array

``````// Math.max() on arrays

var numbers = [4, 1, 2, 55, -9];
var maxNum = Math.max(...numbers);
console.log(maxNum); // 55

// make custom function
function getMaxInArray(arr) {
return Math.max(...arr);
}

numbers = ["19", 4.5, -7];
console.log(getMaxInArray(numbers)); // 19``````

Output

```55
19```

As you can see, we can use the new spread operator `...` to destructure the array in the function call and pass it to `Math.max()` and obtain the largest number.