Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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 Program to Find Largest Element in an Array
Next article icon

JavaScript Program to Find Largest Element in an Array

Last Updated : 11 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array.

Example:

Input : [10, 15, 38, 20, 13];
Output: 38
Here we will see the 38 is the largest elements in the given array.

Several methods can be used to find the largest Element in an Array in Javascript, which are listed below:

Table of Content

  • Using the reduce() Method
  • Using the Spread Operator and Math.max() Method
  • Using for Loop
  • Using the sort() method
  • Using a ForEach Loop

Using the reduce() Method

In this approach, Using the reduce() method, iterate through the array while comparing elements. Accumulate the largest element by updating the accumulator if the current element is greater. Return the largest element.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), initialValue );

Example: In this approach, we are using the above-explained approach.

JavaScript
function largestElement(arr) {     return arr.reduce((largest, current) =>         (current > largest ? current : largest), arr[0]); }  let num1 = [10, 15, 38, 20, 13]; console.log(largestElement(num1));  

Output
38 

Using the Spread Operator and Math.max() Method

In this approach, the spread operator (...) with Math.max(), directly pass array elements as arguments, allowing the method to find and return the largest element in the array.

Syntax:

function largestElement(arr) {
return Math.max(...arr);
};

Example: In this example, we are using the Spread Operator and Math.max() to find the largest number from our given array.

JavaScript
function largestElement(arr) {     return Math.max(...arr); }  const num1 = [10, 15, 28, 20, 13]; const result = largestElement(num1);  console.log("The largest element in the array is:" + result);  

Output
The largest element in the array is:28 

Using for Loop

In this approach, Using a for loop, iterate through the array, comparing each element to a running largest value, updating it if a larger element is encountered, and returning the final largest value.

Syntax:

for (let i = 1; i < arr.length; i++) {
if (arr[i] > largestNum) {
largestNum = arr[i];
}
};

Example: In this approach, we are using for loop to find the largest number from our given array.

JavaScript
function largestElement(arr) {     let largestNum = arr[0];     for (let i = 1; i < arr.length; i++) {         if (arr[i] > largestNum) {             largestNum = arr[i];         }     }     return largestNum; }  const num1 = [10, 15, 18, 20, 23]; const result = largestElement(num1);  console.log("The largest element in the array is:" + result);  

Output
The largest element in the array is:23 

Using the sort() method

In this approach we apply sort() method to array which will sort the array in ascending order. After sorting the largest element will be at the end of the array. We will return the last element which will be the largest element.

Syntax:

arr.sort((a, b) => a - b);
largestElement = arr[arr.length - 1];

Example: In this approach, we are using sort() method to find the largest number from our given array.

JavaScript
function findLargest(arr) {      arr.sort((a, b) => a - b); // Sort the array in ascending order     return arr[arr.length - 1]; // Return the last element }  let arr = [10, 5, 20, 8]; console.log("Largest element in the array is:", findLargest(arr)); 

Output
Largest element in the array is: 20 

Using a ForEach Loop

Using a forEach loop iterates through the array, updating the largest element encountered. It initializes the largest element with the first element, then compares each subsequent element, updating the largest value accordingly.

Example

JavaScript
const array = [10, 5, 20, 8, 15]; let largest = array[0];  array.forEach(element => {     if (element > largest) {         largest = element;     } });  console.log("Largest element:", largest); 

Output
Largest element: 20 



Next Article
JavaScript Program to Find Largest Element in an Array

V

vishalkumar2204
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

    JavaScript Program to Find kth Largest/Smallest Element in an Array
    JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
    5 min read
    JavaScript Program to find the Nth smallest/largest element from an unsorted Array
    In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
    4 min read
    Javascript Program for Third largest element in an array of distinct elements
    Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp
    6 min read
    Javascript Program to Find k maximum elements of array in original order
    Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note: k is always less than or equal to n.Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their appea
    3 min read
    Find the Second Largest Element in an Array in PHP
    We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
    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