Image of a cloud

Cloud Coding

Find Method in JS

By: Tristan Pedersen

Find Method in JS

The find method is applied on arrays. It is a higher order function which means it will take a function in as an argument. find() will execute the function it takes in on each element in the array, it will return the first element that matches the condition of the callback function.

Ex.

const data = [3, 5, 18, 150, 40];
const result = data.find((element) => {
  return element > 10;
});
// returns the first number that is greater than 10
console.log(result);
//output: 18

In the example above the find() method returns the value of the first element that passes a test.

Summary:If the find() method finds an element where the condition is satisfied and the function returns a true value, find() returns the value of that element and does not check the remaining values.

Otherwise, it will return undefined.

Once the condition is satisfied then find() stops iterating over the remaining elements.

find() will not change the original array.