Member-only story
Performing searches through arrays efficiently plays a critical role in any software application. Fortunately, ECMAScript provides two helpful methods — Array.prototype.find()
and Array.prototype.findIndex()
– that significantly simplifies searching within arrays, saving time and computational resources.
This article explains both methods, illustrates their differences, and showcases real-life use cases.
Array.prototype.find(): Quick Overview
The Array.prototype.find()
method locates the first element satisfying the provided predicate function and subsequently returns said element. Its signature looks similar to the following:
array.find(callbackFunction[, thisArg])
Where:
array
refers to the target array over which search is performed.callbackFunction
defines the criteria required for matching an element.thisArg
serves as optional contextual scope for thecallbackFunction
.