How to find the shortest string in an array in JavaScript

Learning JavaScript is crucial for web development. JavaScript arrays are important for organizing data in dynamic web applications, playing a crucial role in client-side operations and server communication. Additionally, mastering basic algorithms, such as finding the shortest string in an array, is foundational, enhancing a programmer’s problem-solving skills for complex tasks in software development. JavaScript does provide a vast variety of building blocks for problem-solving, but there are no built-in functions to find the shortest element in the array. Let's see how to find the shortest element in the array using JavaScript.

Let’s consider the following array consisting of five strings:

0

1

2

3

4

"function"

"variable"

"array"

"algorithm"

"loop"

The goal is to find the shortest string in this array, i.e., “loop.”

Finding the shortest string in an array

Given an array of strings, we can solve this problem as follows:

  1. Select the string at the 0th index of the array as the shortest string

  2. Starting from the 1st index of the array, iterate through the array and for each of the strings:

    1. Compare the length of the string with the shortest string.

    2. If the length of the current string is less than the shortest string, set this current string as the shortest string.

  3. At the end of the loop, we will have the shortest string in the array.

Initializing all variables
Initializing all variables
1 of 5

Code example

const stringArray = ["function", "variable", "array", "algorithm", "loop"];
let shortestString = stringArray[0];
// Starting the loop from index 1 because index 0 is already stored in a variable for comparison
for (let i = 1; i < stringArray.length; i++)
{
const currentString = stringArray[i];
if (currentString.length < shortestString.length) {
shortestString = currentString;
}
}
console.log("The shortest string is:", shortestString);

Code explanation

  • Line 1: We declare a JavaScript array.

  • Line 2: We initialize the variable shortestString with the first element of the array.

  • Line 4: The for loop is created to iterate through the array, starting from the second element, i.e., i = 1. (Since shortestString is initialized with string at index 0, starting the loop at index 1 will save an unnecessary comparison of shortestString with string at index 0 of the array).

  • Line 5: The string at the index i is stored in the currentString variable.

  • Line 7–9: The length of each string is being compared with the current shortestString. If a shorter string is found, shortestString is updated with the currentString.

  • Line 12: The final result is printed to the console.

Here is a dry run of the example illustrating how the algorithm works:

i

shortestString

currentString

currentString.length < shortestString.length

update shortestString

1

function

variable

False

No update

2

function

array

True

shortestString = array

3

array

algorithm

False

No update

4

array

loop

True

shortestString = loop

Time and space complexity:

  • The time complexity of the provided code is O(n)O(n), where nn is the number of elements in the array. This is because the code iterates through the array once.

  • The space complexity is O(n)O(n). Regardless of the size of the array, the amount of additional memory used by the algorithm remains constant, as the only additional variables used are shortestTerm, i, and currentTerm, and they do not depend on the size of the input array.

In conclusion, this simple algorithm allows us to find the shortest string in an array efficiently. This versatile approach can be adapted to similar problems where you need to find the minimum value based on a specific criterion.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved