JavaScript Array includes() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

The Array includes() method in JavaScript is a built-in function used to search for a specific element within an array. Whether it's a number, string, or any other character, includes() performs a case-sensitive search to determine the presence of the specified value in the array. Return type of array includes javascript is boolean, yielding true if the element is found and false if not.

In essence, includes() allows for efficient checking of array contents. When invoked, it returns true if the specified value exists within the array, and false otherwise. This simplicity and effectiveness make it a valuable tool for JavaScript developers when dealing with array operations.

Syntax

The syntax of the array includes() method in javascript is:

Please note arr in the given syntax refers to an array in JavaScript. And, valueToSearch is the value of the element we are searching for in the array.

Parameters

There can be a maximum of two parameters of the array includes javascript method:

  • value: It is the value or the element to look for(required).
  • indexToSearchFrom : It is the index value in the array from which the search for the element should begin(optional).

Return Value

Return Type: Boolean

The includes() method returns a boolean value, either true or false, depending upon the value passed in the method.

If the passed value is present in the specified array, true is returned, else false is returned.

Example of Array includes() in Javascript

Let us see some examples for a better understanding of the includes() method in JavaScript.

Code:

Output:

Explanation:

In the above-given example, we have an array called subjects which has three items Physics, Chemistry and Maths in it.

  • In the first console.log statement, we used the array.includes method in javascript to see if the item Chemistry is present in the array or not, as it was there in the array we got true in the console.
  • In the second console.log statement, we did the same step to check if "Biology" is there in the array, as it was not there we got false in the console.

Array Contains a Primitive Value

There are seven primitive values in JavaScript: string, number, boolean, undefined, null, bigint, and symbol. We can use the includes() method to check if the array contains JavaScript a primitive value or not.

Example:

Code:

Output:

Explanation:

In the above-given example, we have an array called randomArray in which we stored some primitive values in a random order of occurrence. We used the array.includes method in javascript to check if the provided primitive values are present in the array.

  • In the 1st console.log statement, we checked if the string value "orange" is present in the array, as it was there in the array we got true in the console.
  • In the 2nd console.log statement, we checked if the string value "range" is present in the array, as it was not there in the array we got false in the console.
  • In the 3rd console.log statement, we checked if the undefined value undefined is present in the array, as it was there in the array we got true in the console.
  • In the 4th console.log statement, we checked if the number value 2 is present in the array, as it was there in the array we got true in the console.
  • In the 5th console.log statement, we checked if the number value 6 is present in the array, as it was not there in the array we got false in the console.
  • In the 6th console.log statement, we checked if the boolean value false is present in the array, as it was there in the array we got true in the console.
  • In the 7th console.log statement, we checked if the null value null is present in the array, as it was there in the array we got true in the console.

Searching From an Index

We have seen in the Parameters section of this article that we can search for the array element from any applicable desired index value in the array. Let us see some examples of the same.

Code:

Output:

Explanation:

In the above-given example, we have an array called numsArray which contains numbers from 0 to 6. We used the includes() method with the optional argument which is the index from which it starts searching the element till the end of the array.

  • In the 1st console.log statement, we checked the numsArr from index position 2 if it contains the value 1, as it's not present in the given range of array, we got false in the console.
  • In the 2nd console.log statement, we checked the numsArr from index position 1 if it contains the value 1, as it is present in the given range of array, we got true in the console.
  • In the 3rd console.log statement, we checked the numsArr from index position 1 if it contains the value "1", as it is a numeric string that is not present in the given range of array, we got false in the console.
  • In the 4th console.log statement, we checked the numsArr from index position 1 if it contains the value 7, as it is out of the given range of array, we got false in the console.

Passing No Arguments

If we don't pass any arguments in the includes() method, we get false in the output. Let us see an example of the same.

Code:

Output:

Explanation:

In the above-given example, we have numsArray that we created in the previous example. We used the includes() method without passing any argument hence got false in the console.

Array Contains an Object

There can be situations when we want to check if an array contains JavaScript an object having a property equal to the passed value. The question arises, Can we use the includes() method for the same? The answer is "No". Let us understand this with the help of an example:

Code:

Output:

Explanation:

In the above-given example, we used the includes() method to check if the array contains JavaScript the object and an object having the passed property's value. Even though it contains the object and the object with the brand value 'HP', it returned false in both the cases. Hence we can not check this using the includes() method. Let us see how to solve this problem using a simple logical program.

Solution to this problem:

Code:

Output:

Explanation:

In the above-given example, we created a function to find the passed value in the object. We just used a for loop and an if condition to achieve the correct results. The for loop iterates over the whole object sequentially while the if the condition checks for the passed value in each object's property called brand. This is how we can check if an array is an object with the passed value.

Array Contains Another Array Element in JavaScript

We can use some() method in combination with the includes() method to check if the array contains in JavaScript atleast an element same as in another array. Code:

Output:

Explanation:

In the above-given example, we have 3 arrays namely arr1, arr2, and arr3. We used the some() method to check if the array contains another array having at least one element same in both the arrays. As the arr1 and arr2 have one element in common, isPresent1 returned true. In contrast to this, as arr1 and arr3 have nothing in common, isPresent2 returned false.

Javascript Array Contains vs. Includes

There's no built-in method called Array.contains() in JavaScript to check for the element in an array. Instead of this, we have the Array.includes() method to check if an element is present in the array. We have discussed this in detail in the above sections of this article.

Conclusion

  • The array includes() in JavaScript is one of the built-in methods of Arrays used to check if an Array contains the specified value or not.
  • Array includes javascript takes 2 parameters. One is the element to be searched, which is a mandatory parameter. Second is the index from which we can start searching the element in our array, however, it is an optional parameter.
  • The includes() method returns a boolean value, either true or false, depending upon the value passed in the method.
  • If we don't pass any arguments in the includes() method, we get false in the output.
  • We can pass negative indexes in the arrays includes() method in javascript, but at first it will be calculated as array.length+fromIndexarray.length + fromIndex and the result will be used as the starting index. However, if our result is negative then the element will be searched over the entire array.

See Also