From the course: JavaScript: Arrays
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Find a single element with find and findIndex - JavaScript Tutorial
From the course: JavaScript: Arrays
Find a single element with find and findIndex
- [Narrator] The array methods find and findIndex are used when you only need to return a single value within an array. These methods iterate over an array element by element returning the first element or index that passes the test. If you need to return multiple values that meet a specific criteria, you would reach for the filter array method that we will discuss later on in the course. The array methods find and findIndex operate identically but return different values. Both array methods return the first element that satisfies the criteria provided and have identical syntax. Find will return the first element that matches the testing function. And if nothing matches the criteria or the array is empty, undefined will be returned. FindIndex will return the index of the first element that matches the testing function, and if nothing matches the criteria or the array is empty, negative one will be returned. These two…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.