DEV Community 👩‍💻👨‍💻

Cover image for Find the maximum element from the array in javascript
Abu Jaid
Abu Jaid

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Method 3: using spread operator(ES6)

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

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
 }
}
Enter fullscreen mode Exit fullscreen mode

Method 5: using reduce

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

Please visit for more information codewithabu

Top comments (0)

We need you.

We're hiring for a Senior Full Stack Engineer and would love for you to apply. Head here to learn more about who we're looking for.