Find
Lesson 11 of 18 in Coddy's Array Methods in JavaScript course.
The find() method is a built-in function for arrays that searches for the first element that satisfies a provided test condition. It takes a callback function as an argument, which is executed for each element in the array until a truthy value (any value that is not false, 0, "", null, or undefined) is returned. If no element meets the condition, find() returns undefined.
Let's create an array of numbers and use find() to find the first number greater than 5:
const numbers = [1, 3, 7, 2, 9];
const firstNumberGreaterThan5 = numbers.find((number) => number > 5);
console.log(firstNumberGreaterThan5); // Output: 7- Finding the first match:
find()stops iterating as soon as the callback returns a truthy value, making it efficient for finding the first match. - Callback function: This function is the heart of
find(). It takes three arguments:element: The current element being processed in the array.index(optional): The index of the current element.array(optional): The original array thatfind()was called on.
Challenge
EasyGiven scores array is listing marks scored by John.
Your task is to write JavaScript code to find and log the score that is greater than 90.
Try it yourself
const scores = [75, 88, 62, 92, 55];
// Write code here