How to find the shortest string in an array -JavaScript

shortest string representation in front of a code

Finding the shortest string in an array can be a common task when working with strings in JavaScript. There are numerous ways to solve this. In this concise post, we will explore three different easy to use methods for finding the shortest string in an array in a Javascript array. Ready? Let’s check out the first method!

1. .reduce() method

One approach to identifying the shortest string within an array is by utilizing the reduce() function. This function is a built-in feature in JavaScript that accepts a callback function as input. This callback function is used to iterate through each element in the array and return a single value. In our situation, we will use the reduce() function to go through our array of strings (let’s say fruits) and find the shortest one by returning it as a result.

const array = ["apple", "banana", "cherry", "kiwi"];

const shortest = array.reduce(function(a, b) {
  return a.length < b.length ? a : b;
});

console.log(shortest); // "kiwi"

In the code above, we were working with an array of strings and using the reduce() method to go through each element in the array. The callback function that we provided to the reduce method takes two arguments, a, and b, which represent the current and the next element respectively in the array. Inside the callback function, we compared the length of the current element (a) to the next element (b) and returned the element with the shortest length. In this example, the element “kiwi” has the shortest length of 4 characters and it’s returned as the shortest string.

2. .sort() method

The next option to find the shortest string in the array is to by using the sort() method. If you have been coding in JS for a while you’ve probably used it. By default, this method sorts the elements of the array in ascending order. For our purposes, we can use this method to sort the elements of the array by their length. Once the elements are sorted by their length, the first element in the array will be the shortest string. If you only need the shortest string then it’s definitely less efficient than .reduce() method.

const array = ["apple", "banana", "cherry", "kiwi"];

array.sort(function(a, b) {
  return a.length - b.length;
});

console.log(array[0]); // "kiwi"

In this example you can see, we use the sort() method to sort the elements of the array by their length. The callback function takes two arguments, a and b, representing the current and next elements of the array, respectively. Within the callback function, we subtract the length of the next element (b) from the length of the current element (a) to determine the order of the elements.

3. .forEach() method

The last way we will look at how to find the shortest string in a JavaScript array is by using the forEach() method. Same as the last two, .foreEach() is a built-in javascript method that takes a callback function as an input. This function is used to go through each element in the array. Inside the callback function, we can compare the length of the current element to a variable that keeps track of the shortest string. If the current element has a shorter length, we update the variable with the current element.

const array = ["apple", "banana", "cherry", "kiwi"];
let shortest = array[0];

array.forEach(function(element) {
  if (element.length < shortest.length) {
    shortest = element;
  }
});

console.log(shortest); // "kiwi"

This method goes through each string of the array. The forEach() compares the length of the current element to the length of the “shortest” variable (hence why we need to use let instead of const here since we might need to reassign it. If the current element is shorter than the “shortest” variable, it is the new shortest and is assigned to the “shortest” variable

Conclusion

If your only objective is to find the shortest string use the first option. That’s all I have.

Thanks for reading.

Leave a Comment

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