Returns the value of the first element in the array that satisfies the provided testing function
undefined
is returned if no element satisfies testing function
The callback function for .find()
accepts the following parameters:
element
: (required) current element being processed in the array.
index
: (optional) index of the current element being processed in the array
array
: (optional) the array map was called upon
.find()
only returns a single value and does not return an array
Using traditional (ES5) function syntax
const result = arr.find(function(item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
})
Using arrow syntax (ES6)
const result = arr.find((item, index, array) => {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
})
const numbers = [5, 3, 13, 4, 11 ]
const greaterThan5 = numbers.find((number) => {
return number > 5
})
console.log(greaterThan5) // 13
.find()
to return the first word that has more than 6 characters in the words
array. Save the returned values to a variable declared with const
called longWord