The Find & FindIndex Method
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 58 of 65.
The find() method returns the first element in an array that satisfies a provided testing function. If no element is found, it returns undefined.
The findIndex() method is similar but returns the index of the first matching element instead of the element itself. If no element is found, it returns -1.
Basic syntax:
array.find(callback(element[, index[, array]]))
array.findIndex(callback(element[, index[, array]]))Simple example with find():
const numbers = [5, 12, 8, 130, 44];
const found = numbers.find(num => num > 10);
console.log(found);
// 12 (first number greater than 10)Same example with findIndex():
const numbers = [5, 12, 8, 130, 44];
const foundIndex = numbers.findIndex(num => num > 10);
console.log(foundIndex);
// 1 (index of first number greater than 10)Finding objects in an array:
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'oranges', quantity: 5}
];
// Using find
const result = inventory.find(item => item.name === 'bananas');
console.log(result); // {name: 'bananas', quantity: 0}
// Using findIndex
const index = inventory.findIndex(item => item.name === 'bananas');
console.log(index); // 1This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
The find() method returns the first element that satisfies a testing function, or undefined if none found:
const numbers = [5, 12, 8, 130, 44];
const found = numbers.find(num => num > 10);
console.log(found); // 12The findIndex() method returns the index of the first matching element, or -1 if none found:
const numbers = [5, 12, 8, 130, 44];
const foundIndex = numbers.findIndex(num => num > 10);
console.log(foundIndex); // 1Finding objects in arrays:
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'oranges', quantity: 5}
];
const result = inventory.find(item => item.name === 'bananas');
// {name: 'bananas', quantity: 0}
const index = inventory.findIndex(item => item.name === 'bananas');
// 1Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys6Sets Part 2
Math - Union Of SetsMath - Intersection Of SetsMath - Difference Of SetsMath - Symmetric DifferenceSubsets And SuperSetsRecap - Group Friends9Advanced Array methods
The Join & Split MethodsThe Map Method The Filter MethodThe Reduce MethodChaining Array MethodsForEach And Map Are Same?The Sort MethodThe Find & FindIndex MethodRecap - The Chain Master