JavaScript Array indexOf() is a built-in function that searches for a specified element and returns its position. The position here is the index of the element, and if the element does not exist, it returns -1.
const festivals = ['holi', 'diwali', 'christmas']; console.log(festivals.indexOf('diwali')); // Output: 1
In this code, we are searching for the position of the “diwali” element, and it is at position 1 or index 1. The array index starts with 0.
Syntax
array.indexOf(searchElement, fromIndex)
Parameters
Argument | Description |
searchElement (required) | It represents the value to search for in the array. |
fromIndex (optional) | It represents an integer specifying the index at which to begin the search.
By default, its value is 0 if it is omitted. Non-integer values are coerced to integers (e.g., 1.5 becomes 1). |
Element does not exist
If the element’s position you are trying to locate does not exist, it returns -1.
const festivals = ['holi', 'diwali', 'christmas']; console.log(festivals.indexOf('rakhi')); // Output: -1
You can use this approach, which allows conditional checks like if (festivals.indexOf(‘rakhi’) !== -1), to verify the existence of an input element.
Multiple occurrences
If multiple elements with the same value exist in the array being searched, it only returns the first index.
const festivals = ['holi', 'diwali', 'christmas', 'diwali']; console.log(festivals.indexOf('diwali')); // Output: 1
The “diwali” exists at positions 1 and 3, but it only returns the index of the first element, and hence it returns 1 in the output.
Using fromIndex (Start mid-array)
If you want to skip the earlier matches, you can use the fromIndex argument. For example, if you wish to get the second occurrence’s index, you can pass the fromIndex argument.
const festivals = ['holi', 'diwali', 'christmas', 'diwali']; console.log(festivals.indexOf('diwali', 2)); // Output: 3
Negative fromIndex
If fromIndex is negative, it is interpreted as: startIndex = array.length + fromIndex
For example, if the array length is 4 and fromIndex is -1, the startIndex should be 4-1 = 3.
const festivals = ['holi', 'diwali', 'christmas', 'diwali']; console.log(festivals.indexOf('diwali', -1)); // Output: 3
In this code, we started searching from index 3, and at the same position, “diwali” was found, so it returns 3 as the output.
fromIndex greater than array length
If the fromIndex is greater than the array length, it returns -1 because no search occurs, and hence no element is found.
const festivals = ['holi', 'diwali', 'christmas', 'diwali']; console.log(festivals.indexOf('diwali', 5)); // Output: -1
Searching for NaN
You can’t search for a NaN value in the array because NaN !== NaN. Use an array.some(x => Number.isNaN(x)) instead.
const nan_festivals = ['holi', 'diwali', 'christmas', NaN]; console.log(nan_festivals.indexOf(NaN)); // Output: -1
Empty array
If the array is empty, there is nothing to search for, and it returns -1.
const empty_array = []; console.log(empty_array.indexOf('data')); // Output: -1
That’s all!