How to Use the Math.max and Math.min Methods in JavaScript?

Estimated read time 1 min read

The Math.max() and Math.min() methods in JavaScript are used to find the maximum and minimum values in a list of numbers, respectively.

Here’s an example of how to use the Math.max() method:

let numbers = [1, 2, 3, 4, 5];
let max = Math.max(...numbers);

console.log(max);

This will output 5, which is the maximum value in the numbers array.

And here’s an example of how to use the Math.min() method:

let numbers = [1, 2, 3, 4, 5];
let min = Math.min(...numbers);

console.log(min);

This will output 1, which is the minimum value in the numbers array.

Note that in both cases, the spread operator (...) is used to spread the elements of the array into separate arguments for the Math.max() or Math.min() method.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply