Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript - Get first and last Item of JS Array
Next article icon

Find the min/max element of an Array using JavaScript

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.

JavaScript offers several methods to achieve this, each with its advantages.

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.

JavaScript
let Arr = [50, 60, 20, 10, 40]; let minVal = Math.min(...Arr);  let maxVal = Math.max(...Arr); console.log("Min Elem is:" + minVal); console.log("Max Elem is:" + maxVal); 

Output
Minimum element is:10 Maximum Element is:60 
  • Math.min(…Arr) and Math.max(…Arr) find the smallest (10) and largest (60) values in [10, 20, 30, 40, 50, 60], logging “Min Elem is: 10” and “Max Elem is: 60”.

Iterating through the Array 

Iterate through the array, initializing the minimum and maximum values to Infinity and -Infinity, respectively.

JavaScript
let Arr = [50, 60, 20, 10, 40]; let minVal = Infi; let maxVal = -Infi;  for (let item of Arr) {  // Find min val   if (item < minVal)       minVal = item;  // Find max val    if (item > maxVal)        maxVal = item; console.log("Min elem is:" + minVal); console.log("Min elem is:" + maxVal); } 

Output
Min elem is:50 Min elem is:50 Min elem is:50 Min elem is:60 Min elem is:20 Min elem is:60 Min elem is:10 Min elem is:60 Min elem is:10 Min elem is:60 
  • minVal and maxVal are initialized to Infinity and -Infinity.
  • The for...of loop updates minVal and maxVal by comparing each array element.
  • The console.log statements print the minimum and maximum values during every iteration (which should be outside the loop).

Using a Custom Comparator with Array.sort() Method

The Array.sort() method sorts the elements of an array in place and returns the sorted array..

JavaScript
let Arr = [50, 60, 20, 10, 40]; Arr.sort((a, b) => a - b);  let minVal = Arr[0];  Arr.sort((a, b) => b - a);  let maxVal = Arr[0];    console.log("Min Elem is:" + minVal); console.log("Max Elem is:" + maxVal); 

Output
Minimum element is:10 Maximum Element is:60 
  • The array is sorted in ascending order for the minimum and descending order for the maximum.
  • The first element after each sort gives the result, but sorting modifies the array.


Next Article
JavaScript - Get first and last Item of JS Array
author
sayantanm19
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

  • How to Add Elements to a JavaScript Array?
    Here are different ways to add elements to an array in JavaScript. 1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array. Syntax array.push( element1, element2, . . ., elementN );[GFGTABS] JavaScript const arr = [10, 20, 30,
    3 min read
  • Create an Array of Given Size in JavaScript
    The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values. Syntax cosnt arr = new Array( length )
    4 min read
  • Insert at the Beginning of an Array in JavaScript
    Following are different ways to add new elements at the beginning of an array 1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
    2 min read
  • Remove Elements From a JavaScript Array
    Here are the various methods to remove elements from a JavaScript Array 1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed. [GFGTABS] javascript let a = ["Apple",
    6 min read
  • Remove Duplicate Elements from JavaScript Array
    To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
    3 min read
  • How to Remove Multiple Elements from Array in JavaScript?
    Here are various methods to remove multiple elements from an array in JavaScript 1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array. [GFGTABS] JavaScript let a = [1, 2, 3, 4, 5]; let remove = [2, 4
    4 min read
  • Insert at the Beginning of an Array in JavaScript
    Following are different ways to add new elements at the beginning of an array 1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
    2 min read
  • Reverse an Array in JavaScript
    Here are the different methods to reverse an array in JavaScript 1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array. [GFGTABS] JavaScript let
    3 min read
  • JavaScript - Delete last Occurrence from JS Array
    These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. [GFGTABS] JavaScript let
    2 min read
  • Remove Empty Elements from an Array in JavaScript
    Here are different approaches to remove empty elements from an Array in JavaScript. 1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions. array.filter( callback( element, index, arr ), thisValue
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences