Open In App

How to get the longest string in an array using JavaScript ?

Last Updated : 02 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, the task is to get the longest string from the array. Here are a few of the most used techniques discussed with the help of JavaScript. In this article, we will use JavaScript methods to find out the longest string in the array. All approaches are described below with examples. 

Approaches to Get the Longest String in an Array:

  • using the .sort() method
  • using the .reduce() method
  • using JavaScript for loop

Approach 1: Using the .sort() method

In this approach, we will use the .sort() method which calls a function on every 2 elements of the array. It takes ‘a’ and ‘b’ 2 arguments and compares their length. If the answer is positive then ‘b’ is greater else ‘a’ is greater. This method arranges the elements in the decreasing order of their length and we can access the first element by [0].

Example: This example implements the above approach. 

Javascript




// Input array of strings
let arr = [
    "A_Copmuter_Science_Portal",
    "GeeksforGeeks",
    "GFG",
    "geeks",
];
 
// It compares the length of an element with
// every other element and after sorting
// them in decreasing order it returns the
// first element.
function gfg_Run() {
    return arr.sort(function (a, b) {
        return b.length - a.length;
    })[0];
}
 
// Display output
console.log(gfg_Run());


Output

A_Copmuter_Science_Portal


Approach 2: Using the .reduce() method

In this approach, we will use the .reduce() method which calls a function on every 2 elements of the array. It takes ‘a’ and ‘b’ 2 arguments and compares their length. It returns the elements which have a length greater than every element.

Example: This example implements the above approach. 

Javascript




// Input array of strings
let arr = [
    "A_Copmuter_Science_Portal",
    "GeeksforGeeks",
    "GFG",
    "geeks",
];
 
// It compares the length of a element with
// every other element and return it if its
// greater than every other element.
function gfg_Run() {
    return arr.reduce(function (a, b) {
        return a.length > b.length ? a : b;
    });
}
 
// Display output
console.log(gfg_Run());


Output

A_Copmuter_Science_Portal


Approach 3: Using JavaScript for loop

In this approach, we will use JavaScript for loop to traverse through the array and find the longest string comparing each element.

Example: This example implements the above approach.

Javascript




// Input array of strings
let arr = [
    "A_Copmuter_Science_Portal",
    "GeeksforGeeks",
    "GFG",
    "geeks",
];
 
// It compares the length of a string with
// every other string and return it if its
// greater than every other string.
function gfg_Run() {
    let longestString = "";
    for (let i = 0; i < arr.length; i++) {
        if (
            typeof arr[i] === "string" &&
            arr[i].length > longestString.length
        ) {
            longestString = arr[i];
        }
    }
    return longestString;
}
 
// Display output
console.log(gfg_Run());


Output

A_Copmuter_Science_Portal




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads