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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read