Updated May 24, 2023
Introduction to JavaScript Array Contain
In JavaScript, an array is a collection of objects with similar data types. To check whether an array contains a specific element, we can use the JavaScript method called Array.prototype.includes(). This method examines all entries and elements within the array and returns a boolean value, either true or false, based on whether the specified value is found in the array. If the array contains the specified valued element, it returns true otherwise false.
In this article, we will learn about the determination of whether a javascript array contains a particular element or not using Array.prototype.includes() or put includes a () method for arrays, its syntax, working, and some examples that will help us clear the concept of working of this method.
Syntax of JavaScript Array Contain
sampleArray.includes(valueToSearch[, frmIndex])
The above syntax of includes() method is explained in detail below:
- sampleArray: It can be an array variable containing any number of elements in which you want to determine whether it contains a particular value or not.
- ValueToSearch: This is the value that you want to search in the sample array to determine its presence or absence in the array. The value of the element should match the value to search. When performing a string or character comparison using the includes() method, the specified value is searched case-sensitively. The includes() method matches and determines the containment of the value based on its exact case-sensitive format.
- FrmIndex: This is the index value from which you want to search for the element in the array. It determines the starting point in the array for searching for its containment. This is an optional parameter and does not require one. By default, the index has zero (0) value. When searching for a specific value in an array to determine its presence, the element may either be found or not found.
Output: The includes() method returns a boolean value indicating whether the specified value is found in the given array or a specific part of the array, starting from the index parameter provided for the search. It returns true if the value is found and false if it is not found. The includes() method treats all zero values, whether positive or negative (e.g., -0, 0, or +0), as the same during the search. However, it treats the value false and the numeric value 0 differently while performing the search.
Internally, the Array.prototype.includes() uses the same value zero algorithms to search for the presence of the element value in the array.
Examples of JavaScript Array Contain
Let us consider a few examples of how the includes() method can be used to determine the presence of a particular value in the specified array.
Example #1 – Numeric Array
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Demonstration of Array includes() for numeric array</h1>
<p>In this example, we will check whether the array contains 20000 string as element value</p>
<p id="sample"></p>
<script>
var salaries = [8000, 9000, 1000, 1500, 2000, 10000, 15000, 20000];
var searchResult = salaries.includes(20000);
document.getElementById("sample").innerHTML = searchResult;
</script>
</body>
</html>
Output:
Example #2 – String Array
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Demonstration of Array includes() for string array</h1>
<p>In this example, we will check whether the array contains "Java" string as element value</p>
<p id="sample"></p>
<script>
var technologies = ["Java", "Angular", "Hibernate", "Maven", "Javascript", "HTML", "CSS"];
var searchResult = technologies.includes("Java");
document.getElementById("sample").innerHTML = searchResult;
</script>
</body>
</html>
Output:
Example #3 – String Search in Part of the Array
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Demonstration of Array includes() for string array</h1>
<p>In this example, we will check whether the array contains "Hibernate" string as element value</p>
<p id="sample"></p>
<script>
var technologies = ["Java", "Angular", "Hibernate", "Maven", "Javascript", "HTML", "CSS"];
var searchResult = technologies.includes("Hibernate",3);
document.getElementById("sample").innerHTML = searchResult;
</script>
</body>
</html>
Output:
Instead of 3 index if we use var searchResult = technologies.includes(“Hibernate”,2);
then it gives the following output after the execution –
Example #4 – Number Search in Part of The Array
Code:
<!DOCTYPE html>
<html>
<body>
<h1>Demonstration of Array includes() for numeric array</h1>
<p>In this example, we will check whether the array contains 20000 string as element value</p>
<p id="sample"></p>
<script>
var salaries = [8000, 20000, 1000, 1500, 2000, 10000, 15000, 25000];
var searchResult = salaries.includes(20000,1);
document.getElementById("sample").innerHTML = searchResult;
</script>
</body>
</html>
Output:
Instead of 1, if we give the index in the following manner, then the result varies.
var searchResult = salaries.includes(20000,2);
that gives the following output after the execution-
Conclusion
Array.prototype.includes() method helps determine whether the particular element value is present in the specific array in Javascript. Hence, whether an array contains a particular element or not can be determined easily by using this method in javascript.
Recommended Articles
We hope that this EDUCBA information on “JavaScript Array Contain” was beneficial to you. You can view EDUCBA’s recommended articles for more information.