Answers for "how to return the max and min of an array in javascript"

14

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
4

js max value of array

let numbers = [4, 13, 27, 0, -5]; // Get max value of an array in Javascript

Math.max.apply(null, numbers); // returns 27
Posted by: Guest on March-25-2020
1

javascript max array

var values = [3, 5, 6, 1, 4];

var max_value = Math.max(...values); //6
var min_value = Math.min(...values); //1
Posted by: Guest on August-06-2020
1

how to return the max and min of an array in javascript

function minMax(arr) {
  return [Math.min(...arr), Math.max(...arr)];
}
Posted by: Guest on April-09-2020

Code answers related to "how to return the max and min of an array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language