Javascript Array Includes Method with Example

Javascript Array Includes Method with Example

While building an application, we might be having a scenario where we are using an array and we want to check if that array contains a specific value or object. That’s where Javascript Array Method includes() comes into the play.

Usage

Javascript array method includes() checks whether an array contains an element.

Syntax

 arr.includes(valueToFind[, fromIndex]) 

includes() accepts two arguments.

  1. valueToFind : it’s the value that we want to search.
  2. fromIndex(optional) It’s the position from which the search will start.

Return Value

It returns the boolean value.

If the supplied value(valueToFind) is within a javascript array, it’ll return true, otherwise false.

Now let’s see an example of javascript array includes() method.

Javascript Array includes() example

An element is within an array

var array1=["apple","banana","coconut"];
console.log(array1.includes('coconut'))

Here in the above example, you can see that coconut is within an array called array1. Hence that output would be true.

An element is not within an array

var array1=["apple","banana","coconut"];
console.log(array1.includes('dog'))

In this example, you can see that dog is not within an array called array1. Hence that output would be false.

Using fromIndex | Optional second argument of includes() method

If fromIndex is greater than or equal to the length of the array, It’ll not search the array and it’ll return false.

var array1=["apple","banana","coconut"];
console.log(array1.includes('banana',20))

As you can see in this example, we are searching for “banana” and fromIndex is 20. so it’ll return false because fromIndex is greater than the length of the array.

If fromIndex is less than or equal to the index of the item for which we are searching and if that item is available in the array, it’ll return true.

For example:

var array1=["apple","banana","coconut"];
console.log(array1.includes('banana',0)); //OUTPUT: true
console.log(array1.includes('banana',1)); //OUTPUT: true
console.log(array1.includes('banana',2)); //OUTPUT: false

As you can see in the above code, For the first sentence fromIndex is 0, which is less than the index of item “banana“, Hence the output is true.

Note: Here we’ve searched for banana and it’s available in an array, that’s why the output is true. but if we would have something like this: array1.includes('banana1',0), it would have return false because banana1 is not available in the array.

Case Sensitive

In the case of finding string or character, includes() function is case-sensitive.

For example:

var array1=["apple","banana","coconut"];
console.log(array1.includes('Banana')); //OUTPUT: false
console.log(array1.includes('BANANA')); //OUTPUT: false
console.log(array1.includes('banaNA')); //OUTPUT: false

In this example, Banana, BANANA and banaNA are not the same as banana, all the example will return false.

Javascript Array Contains Object

To check if an array includes an object, we can use one of the following methods.

Using javascript filter method

Javascript filter methods return a new array with all array elements that pass the specified conditions.

var array1=[
    { name: "apple" },
    { name: "banana" },
    { name: "coconut" }
];
var newArray=array1.filter(x=> x.name=='apple');

if(newArray.length > 0){
   console.log('Object is in javascript array');
}
else{
   console.log('Object is not in javascript array');
}

As filter function returns a new array, we’ll need to check the length of the new array to check if the object is in javascript array.

That’s why in the above code we’ve checked the length of newArray. and based on that log is displayed.

Using javascript some method

Javascript some method returns a boolean value if any of the elements in the array pass the specified condition.

var array1=[
   { name: "apple" },
   { name: "banana" },
   { name: "coconut" }
];

if(array1.some(x=> x.name=='apple')){
   console.log('Object is in javascript array');
}
else{
   console.log('Object is not in javascript array');
}

And here javascript some method returns a boolean value, so we can directly use in if loop to check if javascript contains an object as shown in the above code.

Also Read:

Leave a Comment