In JavaScript, the Math.max() function is used to find the maximum value among the numbers provided as arguments. It can accept any number of arguments, and it returns the largest of those values.
Example:
let numbers = [5, 8, 3, 12, 4]; let maxNumber = Math.max(...numbers); console.log(maxNumber); // Output: 12
In this example:
- We have an array 'numbers' containing [5, 8, 3, 12, 4].
- We use the spread syntax ('...') to pass each element of the array as an individual argument to Math.max().
- Math.max() returns the largest value among the elements of the array, which is '12'.
Note: If Math.max() is called with no arguments or with non-numeric values, it returns '-Infinity'. Additionally, Math.max() doesn't work directly with arrays, so we need to use the spread syntax or other techniques to pass the array elements as arguments.