I have a simple JavaScript Array object containing a few numbers.

```
[267, 306, 108]
```

Is there a function that would find the largest number in this array?

Translate

I have a simple JavaScript Array object containing a few numbers.

```
[267, 306, 108]
```

Is there a function that would find the largest number in this array?

All Answers

- How to find largest objects in a SQL Server database?
- math - Algorithm to find Largest prime factor of a number
- performance - How to find the kth largest element in an unsorted array of length n in O(n)?
- algorithm - Write a program to find 100 largest numbers out of an array of 1 billion numbers
- algorithm - Puzzle: Find largest rectangle (maximal rectangle problem)

Resig to the rescue:

Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you're not certain the array is that small.You can use the apply function, to call Math.max:

How it works?

The apply function is used to call another function, with a given context and arguments, provided as an array. The min and max functions can take an arbitrary number of input arguments: Math.max(val1, val2, ..., valN)

So if we call:

The apply function will execute:

Note that the first parameter, the context, is not important for these functions since they are static, they will work regardless of what is passed as the context.

The easiest syntax, with the new spread operator:

Source : Mozilla MDN

I'm no JS expert, but I wanted to see how these methods stack up, so this was good practice for me. I don't know if this is technically the right way to performance test these, but I just ran them one right after another, as you can see in my code.

Sorting and getting the 0th value is by far the worst method (and it modifies the order of your array, which may not be desirable). For the others, the difference is negligible unless you're talking millions of indices.

Average results of five runs with a 100,000-index array of random numbers:4.0392msto run3.3742msto run67.4724msto run6.5804msto run1.6102msto runI've found that for bigger arrays (~100k elements), it actually pays to simply iterate the array with a humble

`for`

loop, performing ~30% better than`Math.max.apply()`

:Benchmark resultsYou could sort the array in descending order and get the first item:

How about this:

how about using Array.reduce ?

Almost all of the answers use

`Math.max.apply()`

which is nice and dandy but has limitations.Function arguments are placed onto stack which has a downside - a limit. So if your array is bigger than limit it will fail with

`RangeError: Maximum call stack size exceeded.`

To find a call stack size I used this code:

It proved to be biggest on FireFox on my machine -

591519. This means that if you array contains more than591519items,`Math.max.apply()`

will result inRangeError.Best solution for this problem is iterative way(credit: https://developer.mozilla.org/):

I have written about this question on my blog here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

Finding max and min value the easy and manual way. This code is much faster than

`Math.max.apply`

; I have tried up to 1000k numbers in array.Yes of course exist:

`Math.max.apply(null,[23,45,67,-45])`

and the result return`67`

;Don't forget that the wrap can be done with

`Function.prototype.bind`

, giving you an "all-native"function.You could also extend

`Array`

to have this function and make it part of every array.You can also use forEach:

Using -

`Array.prototype.reduce()`

is cool!`[267, 306, 108].reduce((acc,val)=> (acc>val)?acc:val)`

where

acc = accumulatorandval = current value;You can try this,

I just started with JS but I think this method would be good:

To find the largest number in an array you just need to use

`Math.max(...arrayName);`

, it works like this:So it basically gets the largest number in the array and outputs it to the console. You put the triple dots cause without it you're basically writing

`Math.max([1, 2, 3, 4, 5, 6]);`

which is wrong.## Find the largest number in a multidimensional array

Run this:

And now try

`[3,10,2].max()`

returns`10`

Find Max and Min value using Bubble SortTry this

As per @Quasimondo's comment, which seems to have been largely missed, the below seems to have the best performance as shown here: https://jsperf.com/finding-maximum-element-in-an-array. Note that while for the array in the question, performance may not have a significant effect, for large arrays performance becomes more important, and again as noted using

`Math.max()`

doesn't even work if the array length is more than 65535. See also this answer.