How to Find the Longest String in an Array in JavaScript

How to Find the Longest String in an Array in JavaScript? Using a loop to iterate through each element of the array, acquire its length, and then evaluate it to the other strings to determine if it is longer, we can use JavaScript to get the longest string in an array.

This post will teach you how to use the array’s built-in methods, such as array.map(), array.filter(), array.reduce(), array.sort(), and spread operator, to find the longest string in a JavaScript ES6 array.

Find the Longest String in an Array in JavaScript Using array.filter()

In this illustration, we are utilizing an array to filter the string array’s element with the longest length. When using the map() method, each element of the array is called with the provided function, returning a new array that is filled with the output.

The callback function defined by the array.filter() method returns a new array with the element that meets the specified condition. Replace the return statement with “return Str[0]” to return an element rather than the array.

var mutiLineStr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

function Find_longStr(myarry) {
    var max = myarry[0].length;
    myarry.map(item => max = Math.max(max, item.length));
    Str = myarry.filter(item => item.length == max);
    return Str;
}

console.log("Longest String in array: ", Find_longStr(mutiLineStr)) 

Output:-

Longest String in array: [ ‘JavaScript’ ]

JavaScript Find Longest String In Array Using the Spread Operator with the array.fillter()

To find the longest string in a string array, we utilized the spread operator (…) in this Javascript application together with the array. map() and array. filter(). This is how JavaScript ES6’s longest string is determined.

var mutiLineStr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

var longestStr_inArr = mutiLineStr => {
    let MaxLng_Str = Math.max(...mutiLineStr.map(item => item.length))
    return mutiLineStr.filter(item => item.length === MaxLng_Str)[0]
}

console.log("Longest String in array: ", longestStr_inArr(mutiLineStr)) 

Output:-

Longest String in array: JavaScript

Find the Longest String in a JavaScript array using a for each loop

A for each loop is used in this JavaScript application to cycle through all of the array’s items in search of the longest string.

The longest string is stored in the variable “largest Str,” which we have defined. The following element is compared to the “largest Str” on each iteration, and the final result is the longest string from the array being iterated over.

var arr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

var largest_Str = '';
arr.forEach(function (item) {
    if (item.length > largest_Str.length)
        largest_Str = item

});

console.log("Longest String: ", largest_Str) 

Output:-

Longest String: JavaScript

Find the Longest String in an Array in JavaScript using for-of loop

The longest string in the array can be found using this JavaScript program. In order to traverse through every element of the array, we are utilizing a for-of-loop.

The longest string from the array is returned when the iteration is complete since we have defined a variable called “largest Str” that stores the longest string during each iteration.

var arr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

var largest_Str = '';
for (let item of arr) {
    if (item.length > largest_Str.length) largest_Str = item
}

console.log("Longest String: ", largest_Str) 

Output:-

Longest String: JavaScript

How To Find The Longest String In An Array JavaScript Using array.reduce()

In order to send in the return result from the calculation on the preceding element, the reduce() method performs a user-supplied “reducer” callback function on each element of the array. A single value is produced by applying the reducer to the entire array of elements.

var arr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

const largest_Str = (arr) => arr.reduce(
    (item, Strlen) => (Strlen.length > item.length ? Strlen : item),
    '',
);

console.log("The output is: ", (largest_Str(arr)))

Output:-

The output is: JavaScript

Find the Longest String in an Array in JavaScript with the array.sort()

In this example, we’ll see how to use the array.sort() method to identify the longest string in a collection of strings.

array.sort() does in-place sorting and returns a fresh array that has been sorted. Ascending is the default sorting order.

var arr = ['SQL', 'C#', 'C++', 'Python', 'JavaScript']

var longest = arr.sort(
    function (a_item, b_item) {
        return b_item.length - a_item.length;
    }
)[0];

console.log("Longest string: ", longest)

Output:-

Longest string: JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *