🤖

find()

🤔
💨
Our robot helper checks each car against a condition!

The find() Method

The find() method searches for the first item that passes a test.

You give it a function (the test), and it returns the first item that makes the function return true.

💡
💨

How find() Works

1const numbers = [5, 12, 8, 130, 44];
2
3// Find first number greater than 10
4const big = numbers.find(num => num > 10);
5console.log(big); // 12
6
7// Find first even number
8const even = numbers.find(num => num % 2 === 0);
9console.log(even); // 12
10
11// If nothing matches, returns undefined
12const huge = numbers.find(num => num > 200);
13console.log(huge); // undefined
🤔
💨

The Arrow Function

The test we give to find() is called a "callback function". Here's what it means:

1// This arrow function:
2num => num > 10
3
4// Is the same as:
5function(num) {
6 return num > 10;
7}
8
9// It checks: "Is this number greater than 10?"
🎮

Try It Yourself!

Click a condition to find the first number that matches! Watch the robot scan each car.

[
5
[0]
12
[1]
8
[2]
130
[3]
44
[4]
]
length: 5