Find the Largest Number in an Array in JavaScript

The quick and easy way to find the largest number in an array in JavaScript.

Published Categorized as JavaScript, Web & Code

Suppose you need to find the largest number in an array in JavaScript. (Which, since you’re reading this post, you probably are.)

What’s the best way to do that?

To find the largest number in an array in JavaScript, use the Math.max() function. When you pass it an array of 0 or more numbers as arguments, it will select and return the largest of them all.

By taking this implementation approach, you’re making use of JavaScript’s built-in Math object and its out-of-the-box Math.max() function. Notice the spelling: the Math object is always spelled with an uppercase “M.” Make sure it’s the same way in your code.

How to Use Math.max()

With the Numbers as Arguments

The simplest, most straightforward way to use the Math.max() function is to give it the numbers as arguments:

// Returns 5
Math.max(1, 2, 3, 4, 5);

// Returns -1
Math.max(-5, -4, -3, -2, -1);

With the Numbers in an Array

If, on more sophisticated implementations, you need to store the numbers in an array, it’s important to know that the Math.max() function doesn’t accept arrays as input.

So, if you were to store the numbers in an array, you’d need the help of the apply() method, which literally takes the array of numbers and applies it as arguments to Math.max():

// Returns 5
let myArray = [1, 2, 3, 4, 5];
Math.max.apply(null, myArray);

// Returns -1
let myArray = [-5, -4, -3, -2, -1];
Math.max.apply(null, myArray);

Things to Know

Browser Compatibility

Math.max() is a built-in ECMAScript, which forms the foundation of the JavaScript language, and it’s supported by virtually all browsers.

When You Give It An Empty Array

If you don’t give any arguments to the Math.max() function (in other words, you don’t feed it with any numbers), it will return negative infinity:

// Returns -Infinity
let myArray = [];
Math.max(myArray);

When You Give It An Array Containing a Non-Number

If one of the arguments that you’re giving the Math.max() function can’t be converted to a number, it will return NaN (which, for readers who are just getting started with JavaScript development, stands for Not-a-Number):

// Returns NaN (Not-a-Number)
let myArray = [1, 2, "three", 4, 5];
Math.max(myArray);

Keep these in mind if you come across any “weird” behavior of your function; you might as well be giving it no arguments or, in certain scenarios, arguments of the wrong data type.

In Conclusion

The Math.max() function is a sleek and efficient way to find the largest number in an array without having to write lines of code and build unnecessary logic for the job.

It introduces no browser compatibility concerns and, as long as you have good control over the inputs as you should, is consistent and reliable. Although there are other solutions out there, this seems to be the best for most implementation scenarios.

Let me and the rest of this post’s readers know in the comments below if you came across any edge cases or limitations that I hadn’t thought of.

Image courtesy of Tomasz Pacyna /Depositphotos

Leave a comment

Your email address will not be published. Required fields are marked *