find, findIndex

Searching arrays.

Examples

find()

Finding the first element greater than 18.

const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
console.log(first); // 25

findIndex()

Finding the index of the first element greater than 18.

const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}
console.log(first); // 3 (index of 25)

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.