5

Given an array of values [1,2,0,-5,8,3], how can I get the maximum value in JavaScript?

I know I can write a loop and keep track of the maximum value, but am looking for a more code-efficient way of doing so with the JavaScript syntax.

byteSlayer
  • 1,806
  • 5
  • 18
  • 36
  • 1
    Second result from 'max of array in javascript' on google ^ – Andrew Li Jul 15 '17 at 23:04
  • Agree, maybe edit the other to include the word armax though... Coming from python, that's what I instictively googled and nothing came up... – byteSlayer Jul 15 '17 at 23:16
  • I would say it has a reasonable title. Regardless of language, I would have search 'max of array in ', but that's just me. – Andrew Li Jul 15 '17 at 23:17

2 Answers2

13

You can use Math.max and ES6 spread syntax (...):

let array = [1, 2, 0, -5, 8, 3];

console.log(Math.max(...array)); //=> 8
gyre
  • 16,369
  • 3
  • 37
  • 47
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • 1
    Or without the spread operator `Math.max.apply(false, arr)` – Khauri Jul 15 '17 at 22:58
  • The spread operator is not **efficient**. – ibrahim mahrir Jul 15 '17 at 23:03
  • 1
    @ibrahimmahrir There is no spread 'operator', [it's syntax](https://stackoverflow.com/a/44934830/5647260). – Andrew Li Jul 15 '17 at 23:06
  • @AndrewLi The three-dot-thing, whatever its real name is..., It's just the worse regarding effeciency. – ibrahim mahrir Jul 15 '17 at 23:07
  • 1
    @ibrahimmahrir Is it? I tested and against `reduce` it sometimes wins and sometimes loses. Plus efficiency shouldn't even matter, we're talking less than 1 millisecond. – Andrew Li Jul 15 '17 at 23:12
  • @AndrewLi Yeah! I don't care about few milliseconds myself. But OP said _efficient way_, so... + When the array get big (talking about seconds) then the spread **syntax** becomes a headache. – ibrahim mahrir Jul 15 '17 at 23:18
  • @ibrahimmahrir Yes, IIRC `Math.max` is also restrained to the max length like `Array#push` is with spread syntax. But `reduce` wouldn't exactly be volumes faster. – Andrew Li Jul 15 '17 at 23:20
  • @AndrewLi I'm not advocating `reduce` neither. `reduce` is also not as efficient as a sweet & simple `for` loop. Last time I did a test: reduce was about `x10` slower than a `for` loop and `spread syntax` was `x40` slower than `for` loop. Thus, if OP is looking for efficiency, he has to use `for`. – ibrahim mahrir Jul 15 '17 at 23:23
  • @ibrahimmahrir By efficiency, what I think the OP meant was a more concise version, than a regular for loop. – Manish Giri Jul 15 '17 at 23:29
  • 1
    @ibrahimmahrir `Math.max(...arr)` will likely be somewhat slower than `Math.max.apply(null, arr)` but nowhere near as 'not efficient'. Neat JS features are almost always a trade-off. If you have other information that can be proven with numbers, please, consider providing an answer in dupe question. Even though this has been discussed on SO countless times, new good answers are always good. – Estus Flask Jul 15 '17 at 23:36
  • `Math.max(..arr)` will typically be compiled into `Math.max.apply`, so there's **no** performance penalty. –  Jul 16 '17 at 12:11
1

You can use the following:

yourArray.reduce((max, value) => {return Math.max(max, value)});

The reduce will itterate over values in the array, at each time returning the maximum of all of the values before the current one.

byteSlayer
  • 1,806
  • 5
  • 18
  • 36