Find index of an element in an array in JavaScript
This post will discuss how to find the index of the first occurrence of a given value in an array in JavaScript.
1. Using Array.prototype.indexOf() function
The indexOf() method returns the index of the first occurrence of the given element in an array and returns -1 when it doesn’t find a match. It uses a strict equality Algorithm (=== operator) to compare elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const arr = [3, 5, 4, 2, 7]; const item = 4; const index = arr.indexOf(item); if (index !== -1) { console.log(`Element ${item} is found at index ${index} in the array`); } else { console.log(`Element ${item} not found in the array.`); } /* Output: Element 4 is found at index 2 in the array */ |
2. Using Array.prototype.findIndex() function
The findIndex() method returns the index of the first occurrence of an element in the array that satisfies the given predicate. It returns -1 when no element is matched. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const arr = [3, 5, 4, 2, 7]; const item = 4; const index = arr.findIndex(x => x === item); if (index !== -1) { console.log(`Element ${item} is found at index ${index} in the array`); } else { console.log(`Element ${item} not found in the array.`); } /* Output: Element 4 is found at index 2 in the array */ |
3. Using Underscore/Lodash Library
The Underscore and Lodash library offer the _.indexOf method, similar to JavaScript’s native .indexOf() method. The following code example demonstrates the usage of the _.indexOf method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
const _ = require('underscore'); const arr = [3, 5, 4, 2, 7]; const item = 4; const index = _.indexOf(arr, item); if (index !== -1) { console.log(`Element ${item} is found at index ${index} in the array`); } else { console.log(`Element ${item} not found in the array.`); } /* Output: Element 4 is found at index 2 in the array */ |
4. Using jQuery
Alternatively, with jQuery, you can use the $.inArray method, which works similarly to JavaScript’s native indexOf() method. The $.inArray() method is a misnomer as it doesn’t return a boolean value but returns the first index of the array element. Its usage is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); const $ = require("jquery")(window); const arr = [3, 5, 4, 2, 7]; const item = 4; const index = $.inArray(item, arr); if (index !== -1) { console.log(`Element ${item} is found at index ${index} in the array`); } else { console.log(`Element ${item} not found in the array.`); } /* Output: Element 4 is found at index 2 in the array */ |
That’s all about finding the index of an element in an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)