Array indexOf in JavaScript

Level 284 keystrokes

indexOf() returns the first index at which a given element is found, or -1 if not found.

JavaScript code

// const declares a binding that cannot be reassigned (the array itself can still change)
// Create an array of fruit names
1const fruits = ["apple", "banana", "cherry"];
// indexOf() finds the position of "banana" in the array (returns 1)
2console.log(fruits.indexOf("banana"));

Output

1
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.

  • "× 8
  • ,× 2
  • ;× 2
  • .× 2
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice