JS Built-ins
← Array70 / 226
ArrayArray.prototype.findintermediate

Array.prototype.find() β€” find string by length

πŸ“– Usage Example
const words = ['cat', 'elephant', 'dog']
words.find(w => w.length > 4)  // β†’ 'elephant'

Use find() to retrieve the first string from an array that meets a length or content condition.

Array.prototype.find() β€” strings

find() works with any element type. You can use it to find strings satisfying a length or content condition.

Challenge: Implement findLongest(strs) that returns the first string with more than 5 characters.

findLongest(['hi', 'hello', 'greetings']) // β†’ 'greetings'
findLongest(['a', 'bb', 'ccc'])           // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ Use `s.length > 5` β€” this excludes strings of exactly 5 characters.
  • β€’ `find()` returns the first match in order, not the longest match.