max of array in JavaScript

13,723

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

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

console.log(Math.max(...array)); //=> 8
Share:
13,723
byteSlayer
Author by

byteSlayer

Hi, I am a front end mobild and web developer, mainly focusing on interactive web apps and real time pear communication. I am also currently a student learning for a degree in computer sciences, mainly focusing on software development. I mainly program in C, Javascript, Objective C and MIPS assembly. I work with ubuntu and my goto() editor in non other than VIM - the one and only.

Updated on June 08, 2022

Comments

  • byteSlayer
    byteSlayer almost 2 years

    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.

    • Andrew Li
      Andrew Li almost 7 years
      Second result from 'max of array in javascript' on google ^
    • byteSlayer
      byteSlayer almost 7 years
      Agree, maybe edit the other to include the word armax though... Coming from python, that's what I instictively googled and nothing came up...
    • Andrew Li
      Andrew Li almost 7 years
      I would say it has a reasonable title. Regardless of language, I would have search 'max of array in <language>', but that's just me.
  • Khauri
    Khauri almost 7 years
    Or without the spread operator Math.max.apply(false, arr)
  • ibrahim mahrir
    ibrahim mahrir almost 7 years
    The spread operator is not efficient.
  • Andrew Li
    Andrew Li almost 7 years
    @ibrahimmahrir There is no spread 'operator', it's syntax.
  • ibrahim mahrir
    ibrahim mahrir almost 7 years
    @AndrewLi The three-dot-thing, whatever its real name is..., It's just the worse regarding effeciency.
  • Andrew Li
    Andrew Li almost 7 years
    Or just ( … ) => … for an implicit return ...
  • Andrew Li
    Andrew Li almost 7 years
    Also, why through the trouble of a reduce operation when spread syntax exists??
  • Andrew Li
    Andrew Li almost 7 years
    @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.
  • ibrahim mahrir
    ibrahim mahrir almost 7 years
    @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.
  • Andrew Li
    Andrew Li almost 7 years
    @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.
  • ibrahim mahrir
    ibrahim mahrir almost 7 years
    @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.
  • Manish Giri
    Manish Giri almost 7 years
    @ibrahimmahrir By efficiency, what I think the OP meant was a more concise version, than a regular for loop.
  • Estus Flask
    Estus Flask almost 7 years
    @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.
  • Bergi
    Bergi almost 7 years
    You might want to provide some reasonable default value that is returned when the array is empty, such a 0, -1 or -Infinity.
  • Admin
    Admin almost 7 years
    Math.max(..arr) will typically be compiled into Math.max.apply, so there's no performance penalty.