JavaScript - Search An Item in an Array
Last Updated :
23 Jan, 2025
Here are the different methods to search for an item in an array in JavaScript
1. Using indexOf()
The indexOf() method searches for a specified value in an array and returns its first index. If the value is not found, it returns -1.
JavaScript
const a = [10, 20, 30, 40];
const index = a.indexOf(30);
console.log(index);
const notFound = a.indexOf(50);
console.log(notFound);
This method works well for primitive data types like numbers or strings but doesn't work for objects.
2. Using includes()
The includes() method checks if an array contains a specific value and returns a boolean (true or false).
JavaScript
const a = [5, 10, 15, 20];
console.log(a.includes(15));
console.log(a.includes(25));
This method is simple and best used when you only need to know if an item exists.
3. Using find()
The find() method searches for an item in an array based on a condition defined in a callback function. It returns the first matching element or undefined if no match is found.
JavaScript
const a = [
{ id: 1, value: 100 },
{ id: 2, value: 200 },
{ id: 3, value: 300 },
];
const res = a.find((item) => item.id === 2);
console.log(res);
Output{ id: 2, value: 200 }
This is ideal for arrays of objects when you need to find an item based on a specific property.
4. Using filter()
The filter() method is used to find all elements in an array that match a condition. It returns an array of matching elements.
JavaScript
const a = [
{ id: 1, value: 50 },
{ id: 2, value: 100 },
{ id: 3, value: 50 },
];
const res = a.filter((item) => item.value === 50);
console.log(res);
Output[ { id: 1, value: 50 }, { id: 3, value: 50 } ]
Use this method when you expect multiple matches.
5. Using some()
The some() method checks if at least one element in the array satisfies a condition. It returns true if a match is found and false otherwise.
JavaScript
const a = [10, 20, 30, 40];
const hasValue = a.some((item) => item > 25);
console.log(hasValue);
This method is useful when you only need to check for the existence of a match.
6. Using findIndex()
The findIndex() method works similarly to find() but returns the index of the first matching element instead of the element itself. If no match is found, it returns -1.
JavaScript
const a = [
{ id: 1, name: "X" },
{ id: 2, name: "Y" },
{ id: 3, name: "Z" },
];
const index = a.findIndex((item) =>
item.id === 2);
console.log(index);
This is helpful when you need the index of the matching element.
7. Using for-of Loop
The for-of loop is another manual approach to search for an item in an array.
JavaScript
const a = [100, 200, 300];
let found = false;
for (const item of a) {
if (item === 200) {
found = true;
break;
}
}
console.log(found);
This method is more readable than the traditional for loop.
Similar Reads
JavaScript - Find an Item in an Array Here are the various methods to find an item in an array in JavaScript1. Using the includes() methodThe includes() method checks if an array includes a certain value, returning the boolean value true or false accordingly.JavaScriptconst a = ['apple', 'banana', 'orange']; console.log(a.includes('bana
4 min read
JavaScript Array indexOf() Method The indexOf() method in JavaScript is used to find the position of the first occurrence of a specific value in an array. If the value is not present, it returns -1. This method is handy for quickly determining where a particular item is located within an array.Syntax:array.indexOf(element, start)Par
3 min read
JavaScript Array find() Method The find() method in JavaScript looks through an array and returns the first item that meets a specific condition you provide. If no item matches, it returns undefined. It skips any empty space in the array and doesnât alter the original array.Syntax:array.find(function(currentValue, index, arr), th
3 min read
JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde
2 min read
JavaScript Array includes() Method The includes() method in JavaScript returns true if an array contains a specified value, and false if the value is not found. This method simplifies checking for the presence of an element within an array, offering a quick and efficient way to return a boolean result. Syntaxarray.includes(searchElem
3 min read
JavaScript - Check if JS Array Includes a Value? To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false.1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the s
4 min read