• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Find the Longest String in an Array in JavaScript

How to Find the Longest String in an Array in JavaScript

May 18, 2022 Leave a Comment

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:

<p>Type a sentence or paragraph below.</p>
<textarea type="text" id="userInput"></textarea>
<div id="click-me" onclick="getLongestWord()">Find longest word</div>
<div id="results"></div>

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.

Find longest word

Full Code:

<p>Type a sentence or paragraph below.</p>
<textarea type="text" id="userInput"></textarea>
<div id="click-me" onclick="getLongestWord()">Find longest word</div>
<div id="results"></div>

<script>

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(){
  var userInput = document.getElementById("userInput").value;
  var userArr = userInput.split(" ");
  document.getElementById("results").textContent = getLongestString(userArr);
};

</script>

Hopefully this article has been useful for you to find the longest string in an array using JavaScript.

Other Articles You'll Also Like:

  • 1.  Remove the First and Last Character From a String in JavaScript
  • 2.  Using JavaScript to Reverse an Array
  • 3.  Using JavaScript to Get the Current Year
  • 4.  Add Years to a Date Using JavaScript
  • 5.  How to Use JavaScript to Change Text in Span
  • 6.  Using JavaScript to Check if String Contains Numbers
  • 7.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 8.  Using JavaScript to Get the Length of Array
  • 9.  Remove Special Characters From String in JavaScript
  • 10.  Using JavaScript to Compare Dates

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy