Introduction to JavaScript Array Contain
An array in javascript is a collection of similar data typed objects. To know whether an array contains a particular element or not, we use includes a method in javascript. Array.prototype.includes() is the method provided in javascript that helps us to determine whether any given array contains the specified value element considering all its entries and elements and this method returns a boolean value either true or false depending on the appropriate result and scenario. In case if the array contains the specified valued element then 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 simply put includes() 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
Syntax of JavaScript Array Contain are given below:
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 the 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 a string or character comparison is to be made then the includes() method considers the specified value to search value in case-sensitive format for matching and determining its containment.
- 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 the optional parameter and not require one. By default, the index has zero (0) value. Whenever a search is made for the particular value for determining whether it is present in an array of not then the element may be either not found or found at the index positive values and numbers from the from the index or at the length of the array plus from index long from the negative values or numbers from the index specified in frmIndex.
Output: The value which is returned from the includes() method is a boolean value that is true if the value to be searched is found in the specified array or the part of the array beginning from the from index parameter specified for beginning the search or false in case if its not found in the array or particular part of the array. All the values of zero whether positive or negative like -0,0 or +0 are treated as the same by the includes() method search policy. However, the False value and 0 are treated differently while searching.
Internally, the Array.prototype.includes() uses the same value zero algorithm 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 particular value in the array that is specified.
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>

4.5 (9,108 ratings)
View Course
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 reult varies.
var searchResult = salaries.includes(20000,2);
that gives the following output after the execution-
Conclusion
Array.prototype.includes() method helps in determining 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. The method returns true if the value is found in the array or part of the array as mentioned in the parameters and false if no such value is present. In the case of strings and characters, the search is made in a case-sensitive manner.
Recommended Articles
This is a guide to JavaScript Array Contain. Here we discuss the introduction and syntax of a javascript array contain along with different examples and its code implementation. You may also have a look at the following articles to learn more –