AppDividend
Latest Code Tutorials

JavaScript Array Contains: The Complete Guide

0

There is no inbuilt array contains() function in JavaScript. To check if an array contains the specific item, use array includes() function.

JavaScript Array Contains

To check if JavaScript array contains the item or not, use array includes() method. JavaScript array includes() is an inbuilt method that checks whether an array contains a specified element. JavaScript includes() method returns true if an array contains an element. Otherwise, it returns false.

Understanding JavaScript array includes()

JavaScript includes() method restricts whether an array contains a particular element among its entries, returning true or false as appropriate.

Syntax

array.includes(element, start)

Parameters

The element is the required parameter, and it is the element to search for.

The start is an optional parameter whose default value is 0 at which position in the array to start the search.

Return Value

The includes() method returns  boolean value.

Example of Array contains in JavaScript

See the following code.

// app.js

let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Adam'))

Output

true

That means Adam is there in the dark array, and it is there. Now, let’s see a condition in which the item is not there in the array.

// app.js

let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Claudia'))

Output

false

Passing the second argument

If the start parameter is greater than or equal to the length of the array, false is returned. The array will not be searched.

// app.js

let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes('Jonas', 6))

Output

false

In this code, the interpreter will start the searching Jonas at index 6, which is not there in the array, and that is why it returns false.

It will only count if you provide the start position, and then if it finds that element, it will return true.

Passing No Arguments

If you don’t pass any arguments to the includes() function, then it will return false.

// app.js

let dark = ['Jonas', 'Martha', 'Mikkel', 'Ulrich', 'Adam', 'Eva']
console.log(dark.includes())

Output

false

We did not know pass which item we are looking for, so it will return true.

Javascript array contains object

To check if Javascript array contains an object with an attribute that equals a given value, there is no inbuilt method to do that. We have to write our logic to check an array contains an Object.

Example

// app.js

let dark = [
  {
    name: 'Jonas',
    friend: 'Martha'
  },
  {
    name: 'Mikkel',
    friend: 'Jonas'
  },
  {
    name: 'Ulrich',
    friend: 'Katerina'
  }
]

let found = false;

for (let i = 0; i < dark.length; i++) {
  if (dark[i].name == 'Ulrich') {
    found = true;
    break;
  }
}

console.log(found)

Output

true

In this example, we defined an array containing three objects. We are checking if an array contains a specific object by comparing its value.

There is no “magic” way to check for something in the array without a loop. Even if you use some higher-order function, the function itself will use the loop. What you can do is break out of the loop as soon as you find what you’re looking for to minimize computational time.

Check if array contains object using filter and arrow function

We can use the combination of Javascript Higher-order functions like array.filter() and arrow function.

// app.js

let dark = [
  {
    name: 'Jonas',
    friend: 'Martha'
  },
  {
    name: 'Mikkel',
    friend: 'Jonas'
  },
  {
    name: 'Ulrich',
    friend: 'Katerina'
  }
]

let found = false;

if (dark.filter(d => d.name === 'Ulrich').length > 0) {
  found = true;
}

console.log(found)

Output

true

Here, we are filtering out the array whose value is Ulrich, and if the array length is > 0, then that means that array includes that item, and we will return true otherwise we will get false.

We can also use an array.some() function. In that case, we don’t have to check the empty array condition.

// app.js

let dark = [
  {
    name: 'Jonas',
    friend: 'Martha'
  },
  {
    name: 'Mikkel',
    friend: 'Jonas'
  },
  {
    name: 'Ulrich',
    friend: 'Katerina'
  }
]

let found = false;

if (dark.some(d => d.name === 'Ulrich')) {
  found = true;
}

console.log(found)

Output

true

It will give us the same output. If the array contains an object, then it returns true otherwise false.

Javascript array contains a string

To check if Javascript contains a string, use array indexOf() function that all arrays have (except Internet Explorer 8 and below) that will return an index of the element in the array, or -1 if it’s not in the array.

JavaScript array indexOf() method searches an array for the specified element and returns its position.

The search will start at the particular position, or the beginning if no start position is determined, and end the search at the end of the array. It returns -1 if the item is not found.

// app.js

let dark = [
   'Jonas',
    'Mikkel',
    'Ulrich',
]

let found = false;

if (dark.indexOf('Jonas') > -1) {
  found = true;
}

console.log(found);

Output

true

In our example, Jonas is included in our array, so it returns true.

Javascript array contains another array

To check if the array contains array in Javascript, use array some(), and array includes() function. The array some() method checks each item of the array against a test method and returns true if any item of the array passes the test function. Otherwise, it returns false.

The indexOf() >= 0 and includes() both return true if the given argument is present in the array.

// app.js

let dark = [
   'Jonas',
    'Mikkel',
    'Ulrich',
]

let dark2 = [
  'Claudia',
  'Noah',
  'Jonas'
]

const found = dark.some(r => dark2.includes(r))
console.log(found);

Output

true

Here, we have used a combination of two functions.

  1. array.some()
  2. array.includes()

It will compare every element from the dark array to dark2 array, and if a single element found in common, then it will return true, and if both arrays are different, then it will return false.

Our example, Jonas found in both arrays, that is why it returns true. Let’s check the other scenario.

// app.js

let dark = [
   'Jonas',
    'Mikkel',
    'Ulrich',
]

let dark2 = [
  'Claudia',
  'Noah',
  'Adam'
]

const found = dark.some(r => dark2.includes(r))
console.log(found);

Output

false

You can see that both arrays’ elements are different, and that is why it returns false.

Javascript array contains vs. includes

JavaScript Array.prototype.includes returns a boolean.

JavaScript Array.prototype.indexOf returns a number.

There is no Array.prototype.contains method native JS. Instead, we use array includes() method to check if the array contains a specific element or not.

Array.indexOf() and String.indexOf() returns -1 on no match, the index/offset on match.

String.includes() and Array.includes() returns boolean true or false.

That is it for how to check if an array contains a specific item or not tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.