In regards to integers JavaScript functionality is far-reaching. Suppose you need to return the highest number from the list or an array. When you have just a few numbers, you can do it manually. But what if you have hundreds of numbers or more extensive data sets? That’s when the Math.max()
function comes into place.
This function allows developers to return the largest of the zero or more numbers from the input. Let’s find out how this function works, review its usual applications, and complete a few exercise tasks by the end of this tutorial.
Basics of Math.max() function
Syntax-wise, the most usual form this function takes looks like this:
Math.max()
Please note that depending on how many numbers you have, this code can also look like this:
Math.max(value0, value1, value2, value3, value4, value5, ...valueN)
Because this function has a straightforward syntax, you'll hardly have any issues with its regular use. Now, it’d be better if you had a picture of how this function operates when we store values in it. Take a look:
console.log(Math.max(12, 2, 45)); // Expected output: 45
In this case, we provide the numbers 12, 2, and 45 and log this function into a console. The expected output you’d see equals 45, which satisfies the requested criteria. Now, let’s move on to more complex cases.
Working with negative numbers
If you start working with positive numbers (integers), it’s relatively easy to determine the maximum value in an array. But what happens when you have a list of negative numbers? Consider the following example:
console.log(Math.max(-6, -13, -1)); // Expected output: -1
As you can see, we’re using the Math.max() function with three negative numbers to find the largest number. But because -1 is the largest one (determined by which integer is closer to 0), the output you get equals this number. While there is no complex logic behind such a code behavior, don’t forget about it when working with the Math.max()
function.
Finding out the largest value in an array
Alright, we’ve already mentioned that the Math.max()
function is fruitful in finding the largest value in a list. But what about arrays? Let’s create an array with numbers and find out the largest value using the Math.max()
function:
const arrayExample = [2, 4, 6, 8, 10, 12];
console.log(Math.max(...arrayExample)); // Expected output: 12
As you see, an array containing the numbers 2, 4, 6, 8, 10, and 12 will obviously return 12 when you use the Math.max()
function. In this example, the function returned the largest value from the array, which consisted of six different integers.
Please note that you can manipulate arrays with numbers, always getting the largest number with the Math.max()
function.
When no numbers provided
To test this function properly, you might want to provide no numbers and use the Math.max()
function to find out the return. Let’s follow this logic and test this code snippet:
const num = Math.max();
console.log(num); // Expected output: -Infinity
In this case, we leave the brackets of the Math.max()
function empty, and the output we get will always be equal “-Infinity.” This word serves as the initial comparant as all numbers are greater than -Infinity.
Non-numeric arguments
When we know that -Infinity is a return, which happens due to empty brackets, you might also want to work with non-numeric arguments. In this instance, let’s include a string, for instance, into a data set and see what output you’ll get. Take a look:
const num = Math.max("JavaScript", 77, 2, 13);
console.log(num); // Expected output: NaN
In this case, the output you get is NaN
, which is explained by the use of a non-numeric argument in the first place. In other words, when working with the Math.max()
function, try to omit using arguments, such as strings. We’ve included the string “JavaScript,” causing the NaN
to return in our example.
A note on Math.min()
A peculiar counterpart to the Math.max()
function is Math.min()
. It returns the smallest of zero or more numbers. Its functionality is similar to the Math.max()
, especially in relation to non-numeric arguments and situations when no numbers are provided. Take a look at a few examples of using the Math.min()
function in real scenarios.
Positive numbers:
const num = Math.min(77, 2, 13);
console.log(num); // Expected output: 2
Negative numbers:
const num = Math.min(-8, 14, 65, -17);
console.log(num); // Expected output: -17
A non-numeric argument:
const num = Math.min("JavaScript", 14, 65, -17);
console.log(num); // Expected output: NaN
Empty brackets:
const num = Math.min();
console.log(num); // Expected output: Infinity
Summary
For the most part, using the Math.max()
function in JavaScript is easy and straightforward. Its primary use is to get the largest number of zero or more numbers. Don’t forget that you can utilize this function when working with arrays, making it a huge advantage for working with vast bulks of data.
Test yourself
Task 1. Using a data set of [7, 12, 67, 12], find out the largest number using the
Math.max()
function:
Solution:
console.log(Math.max(7, 12, 67, 12)); // Expected output: 67
Task 2. Using the same data set as in Task 1, find out the smallest number using a function opposite to
Math.max()
:
Solution:
console.log(Math.min(7, 12, 67, 12)); // Expected output: 7
Task 3. Create a data set and find out the largest number using the
Math.max()
function, including the following data pieces:
String: “JavaScript”
Negative number: -12
Positive numbers: 65, 12, 87
Solution:
console.log(Math.max('JavaScript', -12, 65, 12, 87)); // Expected output: NaN