Yes, the value exists!
The above code prints the given out because the value is already present in the array. It is quite easy to understand that the expected value is present at position 0. Thus, the indexof() method tells you that the value expected is present in the given array.
The includes() method is one such method using which we can easily find out whether the expected value exists in the given array. There are various ways to use include() method. This method returns a Boolean value i.e. true if the value exists and false if it incorrect. The includes() method can be used in various ways to find out if the value exists. To name a few, take a look at the below examples to understand.
In the above method, we have defined two variables as shown. The includes() methods return true because the value which we are looking for is already present in the given array. If the value was not present in the array, the includes() methods might have returned false.
Another way of using the includes() method is by assigning the index value through which the element we are looking for is generated as output. See the below code for reference.
In the above code snippet, we have defined the variable “actors” which the value. We have also defined a variable “names” which would return true or false, if the includes() method returns the shown result. The code above will return true since the value and the index number have been correctly assigned and would return the output.
The above examples are some of the predefined methods that we have used to check whether an element exists in the array or not. We have another approach to find out an array element using loops. Let’s discuss how can we check if the element exists in an array using loops as shown in the below code snippet.
status: Present status: Absent
In the above code snippet, we have defined an array with some values in the form of strings. We have also defined a function under which the variable status is used as a string to mark if the elements are present in the program. The logical flow of the program is to traverse through each element present in the array and check if the element is present. If the value exists in the array it will display “Present” and “Absent” accordingly.