The indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Example 1: Finding an Element
// An array of animals.
const animals = ['ant', 'bison', 'camel', 'duck', 'bison'];
// The index of the first occurrence of 'bison' is found.
const firstBisonIndex = animals.indexOf('bison');
// The index is logged to the console.
console.log(firstBisonIndex); // Output: 1
Explanation: This example finds the index of the first occurrence of the string 'bison'
, which is at index 1.
Example 2: Element Not Found
// An array of colors.
const colors = ['red', 'green', 'blue'];
// The index of 'yellow' is searched, which is not in the array.
const yellowIndex = colors.indexOf('yellow');
// The result is logged.
console.log(yellowIndex); // Output: -1
Explanation: If the element is not found in the array, indexOf()
returns -1
. This is a reliable way to check for the existence of an element.
Example 3: Starting the Search from a Specific Index
// An array with a repeated element.
const elements = ['a', 'b', 'c', 'd', 'a', 'f'];
// The index of 'a' is searched for, starting from index 1.
const secondAIndex = elements.indexOf('a', 1);
// The index of the second 'a' is logged.
console.log(secondAIndex); // Output: 4
Explanation: indexOf()
can take a second argument, fromIndex
, which specifies the index at which to start the search.
Example 4: Finding an Element in a Numeric Array
// An array of numbers.
const numbers = [10, 20, 30, 40, 50, 40];
// The index of the first occurrence of 40 is found.
const fortyIndex = numbers.indexOf(40);
// The index is logged to the console.
console.log(fortyIndex); // Output: 3
Explanation: indexOf()
works with numbers as well as strings, finding the first index of the specified numeric value.
Example 5: Case Sensitivity
// An array of strings with different cases.
const greetings = ['Hello', 'hello', 'HELLO'];
// The index of the lowercase 'hello' is searched.
const lowercaseHelloIndex = greetings.indexOf('hello');
// The index is logged.
console.log(lowercaseHelloIndex); // Output: 1
Explanation: The indexOf()
method performs a case-sensitive search. 'Hello'
and 'hello'
are treated as distinct elements.
Example 6: Using indexOf() to Check for Existence
// An array of permissions.
const userPermissions = ['read', 'write'];
const requiredPermission = 'execute';
// Check if the user has the required permission.
if (userPermissions.indexOf(requiredPermission) === -1) {
console.log('Access Denied');
} else {
console.log('Access Granted');
}
// Output: Access Denied
Explanation: A common use of indexOf()
is to check if an array includes a certain element by seeing if the returned value is not -1
.
Example 7: Finding the Index of an Object (and its limitations)
// An array of objects.
const users = [{ name: 'Alice' }, { name: 'Bob' }];
const userToFind = { name: 'Bob' };
// indexOf does not work as expected with objects.
const bobIndex = users.indexOf(userToFind);
// The result is logged.
console.log(bobIndex); // Output: -1
Explanation: indexOf()
uses strict equality (===
) for comparison. For objects, this means it checks for reference equality, not value equality. Since userToFind
is a different object in memory than the one in the array, indexOf()
returns -1.