The includes() method is represented by the following syntax:
element - The value to be searched.
start - It is optional. It represents the index from where the method starts search.
A Boolean value.
Here, we will understand includes() method through various examples.
Let's see a simple example to determine whether the given array contains the specified element.Test it Now
In this example, we will provide the index from where the search starts.Test it Now
Let's see one more example to determine whether the includes() method is case-sensitive.Test it Now
While searching for primitive value like number or string is relatively easy, searching for objects is slightly more complicated.
In this post, you will read about how to determine if an array contains a particular value, being a primitive or object.
1. Array contains a primitive value
The easiest way to determine if an array contains a primitive value is to use
0 ES2015 array method:
The first argument
1 is the value to search in the array. The second, optional, argument
2 is the index from where to start searching. The method returns a boolean indicating whether
For example, let's determine whether an array of greeting words contains the values
8 because the array contains
1, denoting that
6 is missing in the
1.1 Searching from an index
4 also accepts an optional second argument to start search for value starting an index.
For example, let's start searching from the second item (index
5 and up) in the array:
6 starts searching for
7 letter from index
5. As expected, the letter is found.
1 because the array from index
5 until the end doesn't contain the item
2. Array contains an object
Checking if an array contains an object is slightly more complex than searching for primitive values.
Determining if an array contains a reference to an object is easy — just use the
0 method. For example:
8 because the
3 array contains
7 object reference (which points to the first item of the array).
But more often, instead of searching by reference, you'd like to search for objects by their content. In such a case
0 won't work:
1, because the array doesn't contain
7 object reference. Although the array contains the object
2 that looks exactly like
Ok, so how do you determine if the array contains an object by content, rather than reference? Using
4 method in combination with shallow or deep equality check of objects.
During shallow equality check of objects the list of properties of both objects is checked for equality.
Here's a possible implementation of shallow equality check:
8 in case if both compared objects
8 have the same set of properties with the same values.
In the following code snippet
0 are equal by content, while
2 are not:
As a reminder, the array method
8 if at least one time
5 function returns
Now, let's use the shallow equality function in combination with
3 method to find if the array contains an object by content:
8 checks every item of the array for shallow equality with
If the searched object contains also nested objects, then instead of
0 function you could use the function.
Searching for a primitive value like string or number inside of an array is simple: just use
Determining if an array contains an object by content needs more moving parts. You have to use
3 method combined with shallow equality check:
Note that the presented approaches are not the only ones. E.g. for a long time
3 expression (which is slighlty clumsy) has been used to determine if the
What other ways to detect if an array contains a value do you know?
Like the post? Please share!
Quality posts into your inbox
I regularly publish posts containing:
Subscribe to my newsletter to get them right into your inbox.Subscribe
Join 6946 other subscribers.
About Dmitri Pavlutin
Tech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom 😉.