The includes method for arrays in JavaScript verifies if a particular element is present within the specified array. It yields true if the element is found, and false if it is not.
Syntax
The syntax for the includes method is depicted as follows:
array.includes(element,start)
Parameter
element - The value to be searched.
start - This parameter is not mandatory. It indicates the position in the sequence from which the method begins its search.
Return
A Boolean value.
JavaScript Array includes method example
In this section, we will explore the includes method by examining a range of examples.
Example 1
Let’s examine a straightforward example to ascertain if the provided array includes the designated element.
<script>
var arr=["AngularJS","Node.js","JQuery"]
var result=arr.includes("AngularJS");
document.writeln(result);
</script>
Output:
Example 2
In this instance, we will specify the index at which the search will commence.
<script>
var arr=["AngularJS","Node.js","JQuery"]
var result=arr.includes("AngularJS",1); //returns false, as "AngularJS" is not present after index 1.
document.writeln(result);
</script>
Output:
Example 3
Let’s examine another example to assess if the includes method is case-sensitive.
<script>
var arr=["AngularJS","Node.js","JQuery"]
var result=arr.includes("angularjs");
document.writeln(result);
</script>
Output: