The JavaScript array includes() method checks whether the given array contains the specified element. It returns true if an array contains the element, otherwise false. Show SyntaxThe includes() method is represented by the following syntax: Parameterelement - The value to be searched. start - It is optional. It represents the index from where the method starts search. ReturnA Boolean value. JavaScript Array includes() method exampleHere, we will understand includes() method through various examples. Example 1Let's see a simple example to determine whether the given array contains the specified element. Test it NowOutput: Example 2In this example, we will provide the index from where the search starts. Test it NowOutput: Example 3Let's see one more example to determine whether the includes() method is case-sensitive. Test it NowOutput: JavaScript offers a bunch of useful array methods to check whether an array contains a particular value. 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 valueA primitive value in JavaScript is a string, number, boolean, symbol, and special 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 3 contains 1. For example, let's determine whether an array of greeting words contains the values 5 and 6:
7 returns 8 because the array contains 5 item. But 0 returns 1, denoting that 6 is missing in the 3 array. 1.1 Searching from an index4 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. However, 9 returns 1 because the array from index 5 until the end doesn't contain the item 2. 2. Array contains an objectChecking 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:
4 returns 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:
4 returns 1, because the array doesn't contain 7 object reference. Although the array contains the object 2 that looks exactly like 7. 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:
5 returns 8 in case if both compared objects 7 and 8 have the same set of properties with the same values. In the following code snippet 2 and 0 are equal by content, while 2 and 2 are not:
As a reminder, the array method 3 returns 8 if at least one time 5 function returns 8. 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 7 object. If the searched object contains also nested objects, then instead of 0 function you could use the function. 3. SummarySearching for a primitive value like string or number inside of an array is simple: just use 1 method. 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 3 contains 1. What other ways to detect if an array contains a value do you know? Like the post? Please share!Suggest Improvement Quality posts into your inboxI regularly publish posts containing:
Subscribe to my newsletter to get them right into your inbox. SubscribeJoin 6946 other subscribers. About Dmitri PavlutinTech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom 😉. |