JavaScript

JavaScript Array Contains/Includes

The includes() is a built-in method of JavaScript that is used to discover whether an element is present in an array or not. It can also be used to find substrings in a string. The returning value of the includes() method is a boolean, which means it either returns true or false. This method can be used as a condition for loops and decision-making statements.

In this how-to guide, we will learn how to use the includes() method to find an element within an array; but first, let’s discuss the syntax of includes() in JavaScript.

The syntax of the “Includes” method is mentioned below.

array_name.includes(element, starting_point)

The include() method takes two parameters in JavaScript:

  1. element: The first parameter is required. It is the element that needs to be searched within the array.
  2. starting_point: This parameter is optional. It gives the index at which to start the search. It is 0 by default.

Difference between includes() and contains()

The includes and contains both methods search for a substring within a string or find elements within an array. The includes() is a method present in JavaScript, whereas contains() is not present in JavaScript. It is used in other languages such as Java. The includes() and contains() both have the same functionality, but in JavaScript, this functionality is termed as includes(), while in other programming languages such as Java, it is called contains(). Since this article is about JavaScript, we will only use includes() in our article from here on.

Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:

  • Use the F12 key in Chrome and other chromium-based browsers.
  • Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
  • Use Option + ⌘ + C keyboard shortcut keys in Safari (if the developer menu does not appear, open Preferences by pressing ⌘ +, and in the Advanced tab, check “Show Develop menu in menu bar”).

How to search an element/item in an array using includes method (JavaScript)

Now we will use a couple of examples to show how the includes() method actually works.

var arr = ['John', 'Ron', 'James', 'Jack', 'Chris'];

if(arr.includes('James'))
{
    console.log('The required element found');
}
else
{
    console.log('Could not find the required element');
}

In the above example, we first declared an array and assigned it a few elements. Then we used the includes() method as a condition for the decision making with the if statement. We passed one of the elements of the array as an argument to the includes() method. As that element is present in the array, the includes() method returns true, and the body of the if statement is executed. If the includes() method does not find the element in the array, then the condition would return false, and the body of the else statement would execute.

Variables can also be passed as an argument to the includes() method.

var arr = ['John', 'Ron', 'James', 'Jack', 'Chris'];

let find = 'James';

if(arr.includes(find))
{
    console.log(find);
}
else
{
    console.log('Not Found');
}

The includes() is a case sensitive method:

var arr = ['John', 'Ron', 'James', 'Jack', 'Chris'];

if(arr.includes('james'))
{
    console.log('The required element found');
}
else
{
    console.log('Could not find the required element');
}

Now we will also pass the second parameter to the includes() method, which tells the starting index of the search to the method:

var arr = ['John', 'Ron', 'James', 'Jack', 'Chris'];

if(arr.includes('James', 2))
{
    console.log('The required element found');
}
else
{
    console.log('Could not find the required element');
}
var arr = ['John', 'Ron', 'James', 'Jack', 'Chris'];

if(arr.includes('James', 3))
{
    console.log('The required element found');
}
else
{
    console.log('Could not find the required element');
}

As the first index in an array is 0, the James element is present at the 2nd index. So if we change the starting point of the search to 3, then the includes() method will not be able to find it.

As mentioned above, this method can also be used to search for a string within a string:

var str = 'Hello World!';

if(str.includes('Hello'))
{
    console.log('Success');
}

Conclusion

We can use the includes () method to figure out whether a certain element is present in an array; we can use the includes() method. It is a searching algorithm present in JavaScript which helps when we need to find a substring within a string or an element within an array.

We have learned how to use the includes() method to find an element in an array in this how-to guide. Moreover, we also discussed the type and parameters we can pass to the includes() method.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.