Finding whether an element exists in an Array
A simple example for using include is as follows, there can be nothing simpler as such.
var Numbers = [1,2,3,4,5,6,7]
console.log(Numbers.includes(1)) // True
console.log(Numbers.includes(43)) // False
You can see how the array of numbers can be used to test what is being included in the array. This way it returns back a boolean value telling us whether something exists in the array or not.
Finding whether an element exists in a specified index
In certain cases when we want to see whether there is a data point on a specific index. Let’s have a look at how we can check this out.
var Names = ['bob','clad','jane','mook']
console.log(Names.includes('bob',0)) // True
console.log(Names.includes('jane',3)) // False
As we can see that we can pass the data as well as the index which we want to search for. The index refers to the data at the given index. So in this way you can easily find whether there is a certain data that is contained at a specific index.
Check whether a String includes() a Sub String
You can also check whether a simple string contains a sub string as well. Let’s see a simple example.
var string = "thewebjuice";
console.log(string.includes('web')); // True
Using Includes() as Generic Method
As we have discussed, includes() is a generic function and here we will see the sample of the same. This sample is being taken up from MDN. Now let’s see a sample which tells whether a function has certain arguments.
console.log(.includes.call(arguments, 'a')); // true
console.log(.includes.call(arguments, 'd')); // false
Here you can see how the includes() can check whether there is an argument that is being passed to the function. You can also see the entire application working on the repl.it.
Also published on Medium.