Array Find Method in JavaScript

Level 3133 keystrokes

Uses find() to search for the first element that matches a condition.

JavaScript code

// Create an array of user objects
// Each object has id and name properties
1const users = [
2 { id: 1, name: "Alice" },
3 { id: 2, name: "Bob" }
4];
// find() returns the first element where the callback returns true
// u => u.id === 2 checks if the user's id equals 2
5const user = users.find(u => u.id === 2);
// Print the name property of the found user
6console.log(user.name);

Output

Bob
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • =× 6
  • :× 4
  • .× 4
  • "× 4
  • ,× 3
  • ;× 3
  • (× 2
  • )× 2
  • {× 2
  • }× 2

More JavaScript examples at level 3

← JavaScript typing practice