JavaScript Array.indexOf() Method

Usage — The JavaScript Array.indexOf() method is used to return the first index at which a given element can be found in the array.

It has the following syntax:

JavaScript

arr.indexOf(searchElement[, fromIndex])

Here is an example of using Array.indexOf():

JavaScript

var numbers = [2, 19, 9];

// Return — 0
numbers.indexOf(2);

// Return — -1
numbers.indexOf(7);

Return Value — This method returns the first index at which the given element was found in the array. It returns -1 if the no such element is found.

Additional Information — Array.indexOf() compares searchElement to elements of the array using strict equality (the same method used by the === or triple-equals operator).

Browser Support — The Array.indexOf() method is available in Chrome, Edge, Firefox 1.5+, Internet Explorer 9+, Opera and Safari.

Go Directly To — usage, parameters or working examples.

Parameters

searchElement

The searchElement parameter is used to specify the element to locate in the array. This is a required parameter.

fromIndex

The fromIndex parameter is used to specify the index to start the search at. If the index is greater than or equal to the array’s length, -1 is returned. It simply means the array will not be searched at all. If the provided index value is a negative number, it is taken as the offset from the end of the array.

Please note that even if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched.

This is an optional parameter. Its default value is 0 which implies that the entire array will be searched.

Working Examples

Here are some examples of using the Array.indexOf() method:

JavaScript

var numbers = [12, 9, 19, 9, 18, 7, 7, 5, 9];

// Return — 0
numbers.indexOf(12);

// Return — 5
numbers.indexOf(7);

// Return — 8
numbers.indexOf(9, 4);

// Return — -1
numbers.indexOf(12, -1);

// Return — 2
numbers.indexOf(19, -7);

// Return — -1
numbers.indexOf(12, 4);

Since Array.indexOf() only returns the index of the first match that it finds, you can use the method in the following example to get all the indices which store a particular element.

JavaScript

var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array.indexOf(element);
while (idx != -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}

// Output — [0, 2, 4]
console.log(indices);

The following example shows how you can make sure that a single element is not added to an array more than once.

JavaScript

function updateStudentList(group, student) {
    if (group.indexOf(student) === -1) {
        group.push(student);
        console.log('Now, the group has : ' + group);
    } else if (group.indexOf(student) > -1) {
        console.log(student + ' is already in the group.');
    }
}

var group = ['Andy', 'Sheila', 'Tina', 'Tom', 'Jacob'];

// Output — Now, the group has : Andy,Sheila,Tina,Tom,Jacob,Mindy
updateStudentList(group, 'Mindy');

// Output — Tom is already in the group.
updateStudentList(group, 'Tom'); 

Further Reading

  1. You can read more about the JavaScript Array.indexOf() method on MDN.
  2. You can also take a look at the defeinition of JavaScript Array.indexOf() method in the ECMAScript 2015 specification.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top