---
title: JavaScript Array includes() Method - Scaler Topics
description: Discover how to efficiently check for the presence of elements in JavaScript arrays using the includes() method. Enhance your coding skills by learning this array method.
author: Sufiyan Khan
category: Javascript
---
:::section{.abstract}
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.
:::
:::section{.main}
## Syntax
The syntax of the array includes() method in javascript is:
```javascript
// demonstration of array includes javascript
// To search for any element in the array
arr.includes(valueToSearch)
// To search for an element from any starting index
arr.includes(valueToSearch, fromIndex)
```
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.
:::
:::section{.main}
## 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).
:::
:::section{.main}
## 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.
:::
:::section{.main}
## Example of Array includes() in Javascript
Let us see some examples for a better understanding of the `includes()` method in JavaScript.
**Code:**
```javascript
const subjects = ["Physics", "Chemistry", "Maths"];
console.log(subjects.includes("Chemistry"));
console.log(subjects.includes("Biology"));
```
**Output:**
```plaintext
true
false
```
**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`.
:::
:::section{.main}
## 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:**
```javascript
const randomArray = ["apple", 1, null, 2, "orange", undefined, 54, true, false];
console.log(randomArray.includes("orange"));
console.log(randomArray.includes("range"));
console.log(randomArray.includes(undefined));
console.log(randomArray.includes(2));
console.log(randomArray.includes(6));
console.log(randomArray.includes(false));
console.log(randomArray.includes(null));
```
**Output:**
```plaintext
true
false
true
true
false
true
true
```
**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:**
```javascript
const numsArray = [0, 1, 2, 3, 4, 5, 6];
console.log(numsArray.includes(1, 2)); // 1 is the value we are looking for and 2 is the index postition from where to search for 1
console.log(numsArray.includes(1, 1));
console.log(numsArray.includes("1", 1));
console.log(numsArray.includes(7, 1));
```
**Output:**
```plaintext
false
true
false
false
```
**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`.
:::
:::section{.main}
## 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:**
```javascript
console.log(numsArray.includes());
```
**Output:**
```plaintext
false
```
**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`.
:::
:::section{.main}
## 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:**
```javascript
let myLaptops = [
{
brand: 'Apple',
year: 2021
},
{
brand: 'HP',
year: 2019
},
{
brand: 'Dell',
year: 2018
}
]
const obj = {
brand: 'Apple',
year: 2021
};
console.log(myLaptops.includes(obj));
console.log(myLaptops.includes('HP'));
```
**Output:**
```plaintext
false
false
```
**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:**
```javascript
let myLaptops = [
{
brand: 'Apple',
year: 2021
},
{
brand: 'HP',
year: 2019
},
{
brand: 'Dell',
year: 2018
}
];
// creating a function to find the given brand inside the `myLaptops` array.
function findInmyLaptops(givenBrand){
let isPresent = false; // flagging isPresent as false by default. We will flag it to true only when we find the given brand name in the array.
// traversing the `myLaptops` array using for loop.
for (let i = 0; i < myLaptops.length; i++) {
if (myLaptops[i].brand == givenBrand) {
isPresent = true; // this indicates that the given brand is present in `myLaptops` array.
break;
}
}
// `if` block gets executed when `isPresent` is flagged true, otherwise `else` block gets executed.
if(isPresent){
console.log(givenBrand + " is present in myLaptops");
}else{
console.log(givenBrand + " is not present in myLaptops");
}
}
// calling our newly created function for different brand names to check if they are present in the array or not.
findInmyLaptops('HP');
findInmyLaptops('Samsung');
findInmyLaptops('Apple');
findInmyLaptops('Lenovo');
findInmyLaptops('Dell');
```
**Output:**
```plaintext
HP is present in myLaptops
Samsung is not present in myLaptops
Apple is present in myLaptops
Lenovo is not present in myLaptops
Dell is present in myLaptops
```
**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.
:::
:::section{.main}
## 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:*
```javascript
const arr1 = [1, 2, 3];
const arr2 = [3,4, 5, 6];
const arr3 = [4, 5, 6];
const isPresent1 = arr1.some(elem => arr2.includes(elem));
const isPresent2 = arr1.some(elem => arr3.includes(elem));
console.log(isPresent1);
console.log(isPresent2);
```
**Output:**
```plaintext
true
false
```
**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`.
:::
:::section{.main}
## 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.
:::
:::section{.summary}
## 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 + 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.
:::
:::section{.main}
## See Also
- [Arrays in JavaScript - Scaler Topics](https://www.scaler.com/topics/javascript/javascript-array/)
- [What is an Array of Objects in JavaScript?](https://www.scaler.com/topics/array-of-objects-in-javascript/)
- [filter() in JavaScript - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter "{rel=noopener nofollow}")
- [Compare Two Arrays in JavaScript](https://www.scaler.com/topics/compare-two-arrays-in-javascript/)
:::