JavaScript is a scripting programming language used for both front-end and back-end in web development and it provides a variety of built-in methods to manipulate arrays. In this article, we will see the different methods for checking whether the element is present in the given array or not.
Checking an element that exists in an array is a simple programming task and there are different ways to achieve this in JavaScript. We can find element arrays using both built-in functions and non-built-in functions. There are different approaches to finding an element present in an array, which is described below:
Approach 1: Using includes() method
This method can be utilized to find whether a particular element is present in the array or not. it returns true or false i.e., If the element is present, then it returns true otherwise false.
Example: In this example, the array is declared and by using the includes() method, & we can check the value present in the array.
Javascript
const arr = [21, 12, 13, 45, 54];
if (arr.includes(12)) {
console.log("12 is present in the array.");
} else {
console.log("12 is not present in the array.");
}
Output:
12 is present in the array
Approach 2: Using the indexOf() method
This method can be used to find used the index of the first occurrence of the search element provided as the argument to the method.
Example: In this example, an array is declared and by using indextOf() function, we can check the value present in the array.
Javascript
const arr = [10, 72, 3, 14, 5];
if (arr.indexOf(3) >= 0) {
console.log("3 is present in the array.");
} else {
console.log("3 is not present in the array.");
}
Output:
3 is present in the array
Approach 3: Using the find() method
This method is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of the array and whichever the first element satisfies the condition is going to print.
Example: In this example, an array is declared, and by using the find() function, we can check the value present in the array.
Javascript
const arr = [5, 20, 30, 40, 50];
const result = arr.find(element => element === 30);
if (result !== undefined) {
console.log("30 is present in the array.");
} else {
console.log("30 is not present in the array.");
}
Output:
30 is present in the array
Approach 4: Using the for() Loop
The for loop facilitates the execution of a set of instructions repeatedly until some condition evaluates and becomes false.
Example: In this example, an array is declared by using for() loop iteration up to the element found. By using this, we can check the value present in the array or not.
Javascript
const array1 = [13, 23, 33, 43, 53];
let p = false;
for (let i = 0; i < array1.length; i++) {
if (array1[i] === 53) {
p = true;
break;
}
}
if (p) {
console.log("53 is present in the array.");
} else {
console.log("53 is not present in the array.");
}
Output:
53 is present in the array
Approach 4: Using the Array.some() method
Algorithm:
Initialize the array.
Use some method on for loop with some condition which matches the required element and returns true if matches.
Print the evaluated result.
Example: By using some(), we can check the value present in the array or not.
Javascript
const arr = [5, 20, 30, 40, 50];
const result = arr.some(element => element === 30);
if (result !== undefined) {
console.log("30 is present in the array.");
} else {
console.log("30 is not present in the array.");
}
Output:
30 is present in the array.
Time complexity: O(N), Where N is the length of the Array.
Auxiliary complexity: O(1), Because no extra space is used.
Learn to code easily with our course
Coding for Everyone . This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
25 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...