Open In App

How to Get the Longest String in an Array using JavaScript?

Last Updated : 29 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, the task is to get the longest string from the array in JavaScript. 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. 

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 longestString() {
    return arr.sort(function (a, b) {
        return b.length - a.length;
    })[0];
}

// Display output
console.log(longestString());

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 longestString() {
    return arr.reduce(function (a, b) {
        return a.length > b.length ? a : b;
    });
}

// Display output
console.log(longestString());

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 longestString() {
    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(longestString());

Output
A_Copmuter_Science_Portal


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads