Here’s an example of how to use the
let largest = Math.max(1, 2, 3, 4, 5); console.log("The largest number is: " + largest);
In this example, the
Math.max function is passed the numbers 1, 2, 3, 4, and 5 as arguments. It returns the largest number, which is 5, and the result is logged to the console.
You can also use the
Math.max function with an array of numbers:
let numbers = [1, 2, 3, 4, 5]; let largest = Math.max(...numbers); console.log("The largest number is: " + largest);
In this example, we first declare an array of numbers. Then, we use the spread operator (
...) to spread the elements of the array as individual arguments to the
Math.max function. Finally, the result is logged to the console.