index of max value in array javascript code example
Example 1: how to find index of max number in js
var a = [0, 21, 22, 7];
var indexOfMaxValue = a.reduce((iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0);
document.write("indexOfMaxValue = " + indexOfMaxValue);
Example 2: max value in array javascript
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });
const max = Math.max.apply(null, arr);
const max = Math.max(...arr);
Example 3: js max value of array
let numbers = [4, 13, 27, 0, -5];
Math.max.apply(null, numbers);
Example 4: how to return the max and min of an array in javascript
function minMax(arr) {
return [Math.min(...arr), Math.max(...arr)];
}
Example 5: javascript index of biggest number
arr.indexOf(Math.max(...arr))