In the above example, we have chosen the start position as 2. This is the start index from where we would like to start the search. Here in the includes() method, we have added the optional parameter. Here in the result variable, we will as usual store the result if it is present or not. Var arr=Įxplanation: The above snippet declares as an array that has string values. The result in the above screenshot is false which is as expected. The result that is stored in the variable will be hence false. Here the array does not consist of a cat.
The third statement helps us in displaying the result. Once this result is stored in the mentioned variable you can display the result by making use of document.write(). The second statement is storing the result of includes() in the checkArray variable.
By making use of the includes() method we are checking if the array has ‘cat’ in the array. Here we have an array created with the name ‘animals’.
Var animals = Įxplanation: The above code snippet makes use of a string array. The result which is displayed is hence true. If you check the above screenshot the result of the above code snippet is true as the includes() method finds 2 in the array. The third statement work for this purpose. True will signify that the mentioned element is present in the array and false will signify that it is not present in the array. Post find the result we can print it and get it if it is true or false. The second statement here serves this purpose. Once this array is created it makes use of the includes() method to check if 2 is there in the mentioned array. Examples to Implement JavaScript Array includes()īelow are the examples mentioned: Example #1 – Numeric arrayĮxplanation: The above script first creates an array. If the element is found then it will have to return true or the function will return false. It will look for the number or element from the index position mentioned and traverse the array until the end. If the optional parameter with start position was mentioned then instead of traversing the entire array the method includes() would have started from that position. It will return the value true and this will be stored in the user-defined variable checkInclude. It traverses through the array and looks for the integer 2 in it. That means it will directly start looking for 2 in the array A. It will check if the array contains for the integer 2.
This variable is used to store the result of the include() method. Here in this statement a checkInclude variable is used.