Find the maximum element from the array in javascript

In this article i am going to find the maximum value from the array,i will use multiple approach to find the maximum value from the array.

Mehod 1: using math method

let max_value = [4,10,3,20];
Math.max.apply(0,max_value)// 20

Method 2: sort array in descending order

let max_value = [4,10,3,20];
let sortedArray = max_value.sort((a,b)=>b-a);
sortedArray[0]; // 20

Method 3: using spread operator(ES6)

let max_value = [4,10,3,20];
Math.max(...max_value); // 20

Method 4: using loop

let max_value = [4,10,3,20];
let max = max_value[0];
let i;
let len = max_value.length;
for(i=0;imax){
 max = max_value[i]; // 20
 }
}

Method 5: using reduce

let max_value = [4,10,3,20];
max_value.reduce((a,b)=>Math.max(a,b)); // 20

Please visit for more information codewithabu

17