JavaScript Max Element in an Array (original) (raw)

Last Updated : 19 Nov, 2024

These are the following ways to find the largest element in JS array:

**Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the largest element among them.

1. Using the Spread Operator

The spread operator (...) allows an iterable such as an array to be expanded in places where zero or more arguments or elements are expected. This operator can be used with Math.max() to find the largest element in the array.

**Note: The max() function returns "-infinity" for the empty array.

JavaScript `

const a = [22, 65, 1, 39]; console.log(Math.max(...a));

`

2. Using the filter() Method

The filter() method creates a new array with all elements that pass the test implemented by the provided function. We can use this method to filter out all elements except the largest one.

JavaScript `

const a = [22, 65, 1, 39]; let max = a[0]; a.filter(num => { if (num > max) { max = num; } });

console.log(max);

`

3. U**sing For Loop

let a = [22, 65, 1, 39]; let i; let max = a[0];

for (i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; }

console.log(max);

`

4. U**sing Math.max() and apply() Methods

The JavaScript **Math max() Method is used to return the largest of zero or more numbers. The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number. The apply() function allows you to pass an array of arguments to the Math.max() function.

JavaScript `

const a = [22, 65, 1, 39]; console.log( Math.max.apply(null, a));

`

5. U**sing reduce() Method

The Javascript **arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.

JavaScript `

const arr = [22, 65, 1, 39]; const ans = arr.reduce(function (a, b) { return (a > b) ? a : b }); console.log(ans);

`

6. U**sing sort() Method

The Javascript **array.sort() is an inbuilt method in JavaScript that is used to sort the array. An array can be of any type i.e. string, numbers, characters, etc. Here array is the set of values that are going to be sorted.

JavaScript `

const arr = [22, 65, 1, 39]; arr.sort((a, b) => b - a); console.log(arr[0]);

`

7. U**sing Recursion

function largest(a, n, i) { if (i == n - 1) { return a[i]; }

// Find the maximum from rest of the array
let recMax = largest(a, n, i + 1);

// Compare with i-th element and return
return Math.max(recMax, a[i]);

}

const a = [22, 65, 1, 39]; const n = a.length; console.log(largest(a, n, 0));

`

8. Using the forEach Method

The forEach method iterates over each element in the array, comparing each element to a variable (max) initialized to the first element. If the current element is greater than max, max is updated. After the loop, max holds the largest value.

JavaScript `

let a = [1, 2, 3, 4, 5]; let max = a[0]; a.forEach(num => { if (num > max) { max = num; } }); console.log(max);

`

Similar Reads

Mathematical









Recursion







Array








Searching






Sorting












Hashing



String







Linked List