The Math.max
function in JavaScript is used to find the maximum value among a list of numbers. You can pass any number of arguments to this function, and it will return the largest one.
Here’s an example of how to use the Math.max
function:
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.
+ There are no comments
Add yours