findIndex()

noun

say it: “FYND IN-deks” /faɪnd ˈɪndɛks/

A JavaScript array method that runs a test on each item and gives the index of the first item that passes, or -1 when none does.

What is findIndex() in JavaScript?

findIndex() — An array method that runs a test function on each item and gives the index of the first one that passes, or -1 if none does. Use it when you need where an item is, not only the item, such as to remove it.

const songLengths = [214, 187, 402, 256];
const firstLong = songLengths.findIndex((seconds) => seconds > 300);
console.log(firstLong); // 2

In a sentence

  • “pets.findIndex((pet) => pet.name === 'Biscuit') gave 1, so I knew which slot to update.”

Level: Apprentice · More JavaScript terms at this level

Updated