Find Largest Number in Array JavaScript

The Math.max() method returns the largest of zero or more numbers.


Math.max([value1[, value2[, ...]]])

value1, value2, …
Because the max() method is a static method of the Math object, you should always use it as Math.max() rather than trying to call the method on a created instance of the Math object (since the Math object is not a constructor).

When called without arguments, the result of the call is -Infinity.

If at least one of the arguments cannot be converted to a number, the result will be NaN.

Example: Using the Math.max() method

Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

Finding the maximum element in an array
The following function uses the Function.prototype.apply() method to find the maximum element in a numeric array. Calling getMaxOfArray([1, 2, 3]) is equivalent to calling Math.max(1, 2, 3), but you can use getMaxOfArray() with programmatically constructed arrays of any size. It is recommended to use only if you are processing arrays with a small number of elements.

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);

Recommended Articles