We can find the longest string in an array in JavaScript by using a loop to go over each element of the array, get the length of that element, and compare it to the other strings to see if it is longer. Here is a function that will get the longest string in an array using JavaScript.
function getLongestString(stringArray){
var longestString = "";
for (var i=0; i<stringArray.length; i++){
if (stringArray[i].length > longestString.length){
longestString = stringArray[i];
}
}
return longestString;
};
Let’s see an example of this function when we give it a list of strings.
var arrayOfStrings = ["This","is","a","list","of","some","short","and","some","longer","strings"];
function getLongestString(stringArray){
var longestString = "";
for (var i=0; i<stringArray.length; i++){
if (stringArray[i].length > longestString.length){
longestString = stringArray[i];
}
}
return longestString;
};
console.log(getLongestString(arrayOfStrings));
#Output:
strings
When working with arrays of strings, one piece of information which can be useful is to know the longest string in the array.
Using JavaScript, we can get the longest string in an array of strings pretty easily.
To find the string with the most length, we can loop over the strings in the array, compare the length of the current string to the longest string up until that point, and then if the current string is longer, we make that the new longest string.
Below is our JavaScript function again which will find the longest string in a list.
function getLongestString(stringArray){
var longestString = "";
for (var i=0; i<stringArray.length; i++){
if (stringArray[i].length > longestString.length){
longestString = stringArray[i];
}
}
return longestString;
};
Interactive Example of Finding the Longest String in an Array in JavaScript
In this example, we will let the user pass us a list of strings. We will then create an array given the strings, and let them know which one is the largest. Here is our HTML setup:
Type a sentence or paragraph below.
To make this work on the JavaScript side, we will first need to get the user input from the textarea field.
We then need to convert the input into an array of strings. We do this using the split() method.
Finally, we will enter this array into our function we created above to find the largest string. We then will show that string to the user by updating the results div using the textContent property.
Here is our JavaScript code below:
function getLongestString(stringArray){
var longestString = "";
for (var i=0; i<stringArray.length; i++){
if (stringArray[i].length > longestString.length){
longestString = stringArray[i];
}
}
return longestString;
};
function getLongestWord(){
//Get the user input
var userInput = document.getElementById("userInput").value;
//Convert the user input into an Array of strings
var userArr = userInput.split(" ");
//Pass the array to our function we created and show the result
document.getElementById("results").textContent = getLongestString(userArr);
};
The final code and output for this example of how to find the longest string in an array in JavaScript is below:
Code Output:
Type a sentence or paragraph below.
Full Code:
Type a sentence or paragraph below.
Hopefully this article has been useful for you to find the longest string in an array using JavaScript.