Checking if an array contains a specific value is a common task in JavaScript. Whether you’re searching for numbers, strings, or an object, there are various methods to do so depending on specific situations. In this blog, we will explore different approaches to check if an array includes a value. Also, compare their performances which can help you to choose a better approach based on the use case.
Table of Contents:
Methods to Check if an Array Includes a Value?
Here, are some of the best approaches to check if an array includes a value or not. One point that is important to note is these methods return you either true or false.
Method 1: Using includes() Method
It is one of the simplest ways to check if an array contains a specific value. It returns true if a value is present and false if a value is not present.
Example:
Output:
Explanation: In this example, you’re using .includes() method to check if the element is present inside it or not like in the Courses array “HTML” is not present so it returns false. But “JavaScript” is present so it returns true.
Method 2: Using indexOf()
The indexOf(<element>) method returns the index of a value if it’s found, or it returns -1 if the value is not present.
Example
Output:
Explanation: indexOf() returns -1 when it’s not found “HTML” in the Courses array. Thus, (Courses.indexOf(“HTML”) !== -1) returns false, and “ML” was found at index 2 thus, (Courses.indexOf(“ML”) !== -1) returns true.
Method 3: Using find()
The find() method searches for the element that matches a condition. If it finds a match, returns the element otherwise, it returns undefined.
Example:
Output:
Explanation: find() method is more suitable when you have to check for a specific value in the array of objects like “ML” is not found in the Courses and returns false while “AI” is found in Courses and returns true.
Method 4: Using filter()
filter() method returns an array of elements that match a condition. So, if a result array is not empty then it means the value exists.
Example:
Output:
Explanation: filter() method returns an array of elements that match the given condition. .length property checks whether the array contains some elements or not. If the array is empty then it returns false otherwise it returns true.
Method 5: Using some()
some() method checks for at least one element in the array that meets the condition. If any one match is found it returns true otherwise it returns false.
Example:
Output:
Explanation: It is helpful for checking conditions dynamically, some() method tries to find at least one element that satisfies the condition, and if it matches then returns true. As in the above example, “AI” is found thus it returns true, and “HTML” is not found thus it returns false.
Method 6: Using Set.has()
If you’re working on a large dataset and there is a need to find the specific value in an array using JavaScript then, converting Array into Set and using .has() function is the fastest method.
Example:
Output:
Explanation: Converting an array into a set and using .has() method to find elements is a simple and convenient way when you’re working with large datasets. CoursesSet.has(“JavaScript”) checks for the element “JavaScript” in the Courses set. It returns true because the match is found at index 0. Similarly for “CSS” no match is found thus, returns false.
Method 7: Using a Loop (Linear Search)
If you don’t want to use the predefined methods then, Using for() loop you can make your own method to check whether a specific element is present in the array or not.
Example:
Output:
Explanation: In this example, it loops through each element, and if “ML” is found, it sets the match to true and exits the loop using a break. Finally, it prints the result (true or false) to the console.
Method 8: Using Binary Search
If your array is a sorted array then, Binary Search is the best method for checking whether an element is present in the array or not.
Example:
Output:
Explanation: In this example, the binary search continuously divides the array and finds its mid element and after finding the middle element compares it with the target value. If it finds the target value then returns true, otherwise returns false.
Method | Time Complexity | Best For |
includes() | O(n) | It is good for performing simple checks in JavaScript. |
indexOf() | O(n) | finding an element index and it is supported by older browsers. |
find() | O(n) | Searching in an array of objects. |
filter() | O(n) | Finding multiple occurrences of a value. |
some() | O(n) | Best for conditional-based checks |
Set.has() | O(1) | Helpful in finding when a large dataset is given. |
Linear Search | O(n) | Works everywhere |
Binary Search | O(log n) | Works on sorted arrays. |
Conclusion
we have read about multiple methods till now to check if an array includes a specific value or not. Choosing the right method depends on your use case. if you’re working with small arrays, includes() is the simplest choice of all the methods. For large datasets, Set.has() provides you best performance. Similarly, if you have a sorted array then binary search is most efficient. Understanding these methods helps you to write effective javascript code.
How to Check If an Array Includes a Value in JavaScript? – FAQs
1. Which is the best method to check if an array contains a value?
The includes() method is the best and simplest method to check if an array contains a value or not.
2. Which method is fastest to check for a specific value inside the array?
Set.has() works well with a large amount of dataset while, if you have a sorted array then binary search is the best way.
3. Why is indexOf() used instead of includes() sometimes?
indexOf() is available in older browsers, while includes() is not supported in IE11.
4. How to use includes in JavaScript array?
The includes() method is the simplest way to check if an array contains a specific value. Syntax: array.includes(value, startIndex);
5. What is the In_array function in JavaScript?
Unlike PHP, JavaScript does not have a built-in in_array() function. However, the functionality of in_array() can be replicated using includes(), indexOf(), or other methods.