Let's declare an array X and populate it with strings, animals, cows, and ducks. Now, let's use the include statement to find, if the string cows exist in the array. We'll log this using the console.log statement.
Now if you run this, the output should be true. As you can see, the output is true. Now, let's look at the old method that was used to achieve this.
We used to use the indexer function to find the index of the input in the given array, and check if the index is greater than zero. Now if you go ahead and run this, you can see that both the methods yield the same result.
Now, we'll go ahead and pass in a string that does not exist in the array. Let's pass in a string goats, and if you go ahead and run this, you can see that the statement yields false.
Let's take a look at some more complicated examples. I declared an array X with single object in it that is Not a Number. If we log this statement, we'll get true, since Not a Number is an existing value in the X array.
Contrarily, if we use the older method, the result is false. This might be a handy result to be remembered.
The includes method does not differentiate between a negative zero and a positive zero. Hence, it will return true. The includes method has not only be added for the simple array type. It has also been added to all the typed array types.
In this specific case, we made an eight bit unsigned integer array using the following elements. Now we are checking, if the element eight is included in the array Y. As you can see, the result is true, which means the element eight is present in the array Y.