Get The Min And Max Elements Of An Array In JavaScript

To get the min and max elements of an array in JavaScript, you can use the Math.min() and Math.max() methods, along with the spread operator (…) to spread the array into individual arguments, like this – `Math.min(…arr)` and `Math.max(…arr)`

Let’s see how to do this in detail below.

get the min and max elements of an array in JavaScript

Get The Min And Max Elements Of An Array In JavaScript

To get the min and max elements of an array in JavaScript, you can use the Math.min() and Math.max() methods. The Math.min() method returns the smallest number in an array, while the Math.max() method returns the largest number in an array.

Here’s an example:

const arr = [-5, 10, -3, 12, 8];

console.log(Math.min(...arr)); // -5

console.log(Math.max(...arr)); // 12

As you can see, to use the Math.min() and Math.max() methods, you need to spread the array into individual arguments using the spread operator (…). This is because the Math.min() and Math.max() methods expect individual numbers as arguments, not an array.

If you don’t spread the array into individual arguments, you’ll get NaN:

const arr = [-5, 10, -3, 12, 8];

console.log(Math.min(arr)); //NaN

To fix this, make sure to spread the array into individual arguments like this – `Math.min(…arr)` and `Math.max(…arr)`.

Get The Min And Max Elements Of An Array In Internet Explorer

If you need to support Internet Explorer (IE), you can use the Math.min.apply() and Math.max.apply() methods, like this:

const arr = [-5, 10, -3, 12, 8];

console.log(Math.min.apply(null, arr)); // -5

console.log(Math.max.apply(null, arr)); // 12

As you can see, the Math.min.apply() and Math.max.apply() methods expect the array as the second argument. The first argument is the this value, which we don’t need here, so we pass in null.

The second argument is the array that we want to find the min and max elements of.

Internally, the apply() method calls the min() or max() method with the array elements as arguments.

Get The Min And Max Elements Of An Array In Older browsers

If you need to support older browsers, you can use the reduce() method, like this:

const arr = [-5, 10, -3, 12, 8];

const min = arr.reduce((a, b) => {

  return Math.min(a, b);

});

const max = arr.reduce((a, b) => {

  return Math.max(a, b);

});

console.log(min); // -5

console.log(max); // 12

As you can see, the reduce() method expects a callback function as an argument. This callback function is invoked for each element in the array, and it’s used to calculate the min or max value.

Finally, we print out the min and max values to the console.

Conclusion

To get the min and max elements of an array in JavaScript, you can use the Math.min() and Math.max() methods. You need to spread the array into individual arguments using the spread operator (…) because the Math.min() and Math.max() methods expect individual numbers as arguments, not an array.

If you need to support older browsers, you can use the Math.min.apply() and Math.max.apply() methods, or the reduce() method.

I hope this was helpful! 🙂

Leave a Reply