To find the smallest word in a list using JS, you can use the following code:
const words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const smallestWord = words.reduce((prev, curr) => prev.length < curr.length ? prev : curr);
console.log(smallestWord); // Output: 'date'
In the above code, we have an array of words and we use the reduce()
method to iterate through the array and compare the length of each word in the array. The reduce()
method returns the smallest word in the array by comparing the length of the current word with the previous word. The output of this code will be the smallest word in the list which is 'date'.
Asked: 2021-11-09 11:00:00 +0000
Seen: 16 times
Last updated: Apr 19 '21