JavaScript - Find an Item in an Array
Last Updated :
24 Jan, 2025
Here are the various methods to find an item in an array in JavaScript
1. Using the includes() method
The includes() method checks if an array includes a certain value, returning the boolean value true or false accordingly.
JavaScript
const a = ['apple', 'banana', 'orange'];
console.log(a.includes('banana'));
console.log(a.includes('grapes'));
In this example
- a.includes('banana') checks if the array contains the string 'banana', and returns true because 'banana' is present.
- a.includes('grapes') checks if the array contains the string 'grapes', and returns false because 'grapes' is not in the array.
2. Using the indexOf() method
The indexOf() method returns the index of the first occurrence of a specified value in an array, or -1 if it is not found.
JavaScript
const a = ['apple', 'banana', 'orange'];
console.log(a.indexOf('banana'));
console.log(a.indexOf('grapes'));
In this example
- a.indexOf('banana') returns 1, the index of 'banana' in the array.
- a.indexOf('grapes') returns -1 because 'grapes' is not found in the array.
3. Using the find() method
The find() method returns the value of the first element in an array that satisfies a provided testing function, or undefined if no values satisfy the function.
JavaScript
const a = ['apple', 'banana', 'orange'];
const res = a.find(fruit => fruit === 'banana');
if (res) {
console.log("banana is present in the array");
} else {
console.log("banana is not present in the array");
};
Outputbanana is present in the array
In this example
- The find() method searches for 'banana' in the array and returns the first match, which is 'banana'.
- Since 'banana' is found, the message "banana is present in the array" is logged to the console.
4. Using Array.some()
method
The some()
method tests whether at least one element in the array passes the provided function
JavaScript
const a = [1, 2, 3, 4, 5];
const res = a.some(num => num > 3);
console.log(res);
In this example
- The some() method checks if any element in the array is greater than 3.
- Since 4 and 5 satisfy the condition, the output is true.
5. Using forEach Loop
Using a forEach loop, iterate through each element in the array. Within the loop, check if the current element matches the target item.
JavaScript
let a = [1, 2, 3, 4, 5];
let found = false;
a.forEach(element => {
if (element === 3) {
found = true;
}
});
console.log(found);
In this example
- The forEach() method iterates through the array to check if any element is equal to 3.
- Once it finds 3, it sets found to true, and the result is logged as true.
6. Using the filter() method
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
JavaScript
const a = [1, 2, 3, 4, 5];
const res = a.filter(a => a === 3);
if (res.length > 0) {
console.log("3 is found in the array.");
} else {
console.log("3 is not found in the array.");
}
Output3 is found in the array.
In this example
- The filter() method creates a new array containing elements that are equal to 3.
- If the length of the result array is greater than 0, it logs "3 is found in the array.", otherwise "3 is not found in the array."
7. Using the Set and has() Method
The Set object in JavaScript stores unique values. The has() method checks if a specific value exists in the Set.
JavaScript
const a = new Set(['apple', 'banana', 'orange']);
console.log(a.has('banana'));
console.log(a.has('grapes'));
In this example
- a.has('banana') returns true because 'banana' exists in the Set.
- a.has('grapes') returns false because 'grapes' is not in the Set.
8. Using Array.prototype.reduce()
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
JavaScript
const a = [1, 2, 3, 4, 5];
const tar = 3;
const item = a.reduce((found, item) => {
if (found) {
return true;
}
return item === tar;
}, false);
if (item) {
console.log(`${tar} is found in the array.`);
} else {
console.log(`${tar} is not found in the array.`);
}
Output3 is found in the array.
In this example
- The reduce() method is used to iterate over the array and check if any element equals tar (3).
- If the element matches tar, it returns true; otherwise, it returns false and continues the iteration.
- The result is logged based on whether tar is found in the array.
Similar Reads
JavaScript - Search An Item in an Array Here are the different methods to search for an item in an array in JavaScript1. 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.JavaScriptconst a = [10, 20, 30, 40]; const index = a.indexOf(30); con
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 find() function JavaScript arr.find() function is used to find the first element from the array that satisfies the condition implemented by a function. If more than one element satisfies the condition then the first element satisfying the condition is returned. Syntax: arr.find(function(element, index, array), this
3 min read
JavaScript Array findIndex() Method The findIndex() method in JavaScript is a powerful tool for locating the first element in an array that satisfies a provided testing function. This guide provides a comprehensive overview of how to use findIndex() effectively, enhancing your ability to manipulate arrays. Syntax array.findIndex(funct
5 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 Array findLastIndex() Method The Javascript findLastIndex() method is used to find the index of the last element in an array that matches the specified condition. It works by iterating over the array from the end to the beginning and returning the index of the first element that satisfies the condition specified in a callback f
2 min read
Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A
13 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 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