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
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, 2022Comments
-
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 almost 7 yearsSecond result from 'max of array in javascript' on google ^
-
byteSlayer almost 7 yearsAgree, 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 almost 7 yearsI 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 almost 7 yearsOr without the spread operator
Math.max.apply(false, arr)
-
ibrahim mahrir almost 7 yearsThe spread operator is not efficient.
-
Andrew Li almost 7 years@ibrahimmahrir There is no spread 'operator', it's syntax.
-
ibrahim mahrir almost 7 years@AndrewLi The three-dot-thing, whatever its real name is..., It's just the worse regarding effeciency.
-
Andrew Li almost 7 yearsOr just
( … ) => …
for an implicit return ... -
Andrew Li almost 7 yearsAlso, why through the trouble of a reduce operation when spread syntax exists??
-
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 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 almost 7 years@ibrahimmahrir Yes, IIRC
Math.max
is also restrained to the max length likeArray#push
is with spread syntax. Butreduce
wouldn't exactly be volumes faster. -
ibrahim mahrir almost 7 years@AndrewLi I'm not advocating
reduce
neither.reduce
is also not as efficient as a sweet & simplefor
loop. Last time I did a test: reduce was aboutx10
slower than afor
loop andspread syntax
wasx40
slower thanfor
loop. Thus, if OP is looking for efficiency, he has to usefor
. -
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 almost 7 years@ibrahimmahrir
Math.max(...arr)
will likely be somewhat slower thanMath.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 almost 7 yearsYou might want to provide some reasonable default value that is returned when the array is empty, such a
0
,-1
or-Infinity
. -
Admin almost 7 years
Math.max(..arr)
will typically be compiled intoMath.max.apply
, so there's no performance penalty.