Answers for "best way to get max from array"

30

max value in array javascript

// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });

// For arrays with relatively few elements you can use apply: 
const max = Math.max.apply(null, arr);

// or spread operator:
const max = Math.max(...arr);
Posted by: Guest on November-27-2020

Code answers related to "best way to get max from array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language