How to use Math.min() and Math.max() Methods In Javascript

The Math object’s Math.min() and Math.max() methods are static methods that return the minimum and maximum elements of a given array. The spread(…) operator could pass these functions into an array. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands the array and gives the numbers to the functions. 

Syntax:

minValue = Math.min(...array);
maxValue = Math.max(...array);

Example: We will see the basic implementation of JavaScript Math.min() and Math.max() methods.

Javascript
function findMinMax() {
    let Arr = [50, 60, 20, 10, 40];

    let minValue = Math.min(...Arr);
    let maxValue = Math.max(...Arr);
    
    console.log("Minimum element is:" + minValue);
    console.log("Maximum Element is:" + maxValue);
}

findMinMax()

Output
Minimum element is:10
Maximum Element is:60

Find the min/max element of an Array using JavaScript

Finding the minimum or maximum element of an array in JavaScript involves determining the smallest or largest value from the array’s elements. This operation is crucial for various tasks, such as data analysis, optimization problems, and comparisons within algorithms.

The minimum and maximum elements in an array can be found using the following methods:

Table of Content

  • Using Math.min() and Math.max() Methods 
  • Iterating through the Array 
  • Using JavaScript Array.reduce() method
  • Using apply method
  • Using spread operator

Similar Reads

Using Math.min() and Math.max() Methods

The Math object’s Math.min() and Math.max() methods are static methods that return the minimum and maximum elements of a given array. The spread(…) operator could pass these functions into an array. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands the array and gives the numbers to the functions....

Iterating through the Array

Iterating through the array and keeping track of the minimum and maximum elements. The minimum and maximum element can be kept track by iterating through all the elements in the array and updating the minimum and maximum element up to that point by comparing them to the current minimum and maximum values. The minimum and maximum values are initialized to Infinity and -Infinity....

Using JavaScript Array.reduce() method

TheJavaScript Array.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....

Using apply method

Using apply( ) method we can invoke a given function with different objects....

Using spread operator

The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array....

Contact Us