Introduction to JavScript math max() method
Comparison is at the heart of “almost” everything we do, and with numerical values, they are needed to carry out certain operations. We need to know the maximum or the minimum value within a sequence or series of numbers for certain applications.
Say, we need to know what value was the highest or smallest within our dataset, we need a function or method that does just that. Luckily for us, JavaScript provides a Math
object that allows us to achieve this, and more specifically, we can find the highest or largest value using the Math max
method.
In this article, we will discuss ways to use the JavaScript Math max
method
Using the Math max()
method
As stated earlier, the Math.max()
method returns the largest of the series of numbers that are passed as an argument. Using the Math
max method, we can try three approaches - normal arguments, the reduce
method and the spread
operator - to make use of the Math max
method.
Using normal arguments
We can pass all the numbers we want to compare to the Math.max
method to find the highest or largest value among them
console.log(Math.max(12, 3, 4, 5, 67, 321, 121212));
Output
121212
Though this is simple, it can be unrealistic when dealing with a large set of numbers probably stored within an array, and that’s where the spread
operator comes in.
Using the spread
operator
The spread
operator allows expanding all the elements of an iterable such as an array as arguments to functions. So, with these, we can pass all the elements of the array to the Math.max
function.
const arr = [
12, 34, 56, 7, 121, 12212, 12, 2345, 34565, 454, 343, 3432, 778, 454,
];
const highElement = Math.max(...arr);
console.log(highElement);
Output
34565
Using reduce
method
The reduce
method is a higher-order function that takes a callback function that works on each element where an operation is done in relation to the preceding element to result in a single value. So in our case, the operation that will be done will be the Math.max()
method call.
To illustrate, we will apply the Math.max()
method on the current element and its preceding element by find the highest value between the two values via the reduce
method till we return the highest across all the elements in the array.
const arr = [
12, 34, 56, 7, 121, 12212, 12, 2345, 34565, 454, 343, 3432, 778, 454,
];
const max = arr.reduce((a, b) => Math.max(a, b));
console.log(max);
Output
34565
So, instead of using the spread
operator, we use the reduce
method to apply the Math.max()
method on two numbers (the preceding highest element, and the current element) at a time. Note that at the start of the reduce
method, the preceding element is -Infinity
which is considered the lowest number automatically (we can define it in our expression statement but it’s optional).
Summary
The Math.max
method is a good JavaScript method that allows us to find the highest or largest number value among a series of numeric values and can be approached by using the parameters, the spread
operator, and the reduce
method.
References
Math.max() - JavaScript | MDN (mozilla.org)
Spread syntax (...) - JavaScript | MDN (mozilla.org)
Array.prototype.reduce() - JavaScript | MDN (mozilla.org)