Skip to content
Advertisement

Return index of greatest value in an array

I have this:

JavaScript

What’s the best way to return the index of the highest value into another variable?

Advertisement

Answer

This is probably the best way, since it’s reliable and works on old browsers:

JavaScript

There’s also this one-liner:

JavaScript

It performs twice as many comparisons as necessary and will throw a RangeError on large arrays, though. I’d stick to the function.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement