JavaScript

JavaScript Array.includes() method | Explained

JavaScript provides several array methods, such as Array.includes(), Array.map(), Array.forEach(), etc. These methods serve unique functionality and enable a developer to code more efficiently. So, anyone who wants to become a valuable JavaScript developer must have considerable knowledge of these methods. The significance of Array methods is that these methods make the code look simple, clean, and easy to understand. In this post, we will learn one of the most frequently used array methods named Array.includes() method.

This write-up will provide a profound understanding of the below-listed perceptions regarding the Array.includes() method:

So, let’s begin!

What is Array.includes() in JavaScript

It is a predefined method in JavaScript that is used to check whether the array includes the specified item or not. It is a case-sensitive method which means the Array.includes() method will consider the “array” and “Array” as two different words.

Basic Syntax

The code block given below will provide the basic syntax of JavaScript’s Array.includes() method:

array.includes(item, startIndex);

In the above snippet, “item” is the value to be searched, while startIndex is an optional parameter to specify the starting search position. By default, the value for the startIndex parameter is 0.

What does Array.includes() Method Return

The includes() method will return true if the specified value exists in the array, and it will return false if the value to be searched doesn’t exist in the targeted array.

How to Use Array.includes() Method in JavaScript

Let’s consider a couple of examples for a profound understanding of how the Array.includes() method works in JavaScript:

Example1: In this example, we have a string-type array of five items. We will utilize the Array.includes() method to find some items in the array:

<script>

var items = ["JavaScript", "C", "Python", "Java", "PHP"];

console.log("Item Found: " + items.includes("Java"));

console.log("Item Found: " + items.includes("Ruby"));

console.log("Item Found: " + items.includes("HTML"));

console.log("Item Found: " + items.includes("JavaScript"));

</script>

In the above snippet, we searched for the “Java”, “Ruby”, “HTML”, “JavaScript”; consequently, the Array.includes() method will produce the following output:

The Array.includes() method returned true for “Java” and “JavaScript” and false for the rest of the values.

Example2: Let’s consider the below code snippet to understand how Array.includes method deals with case-sensitivity:

<script>

var items = ["JavaScript", "C", "Python", "Java", "PHP"];

console.log("Item Found: " + items.includes("java"));

</script>

In this example, we used the includes() method to check whether “java” is there in the array or not:

The includes() method returned false, which shows that it is a case-sensitive method.

Example1: let’s consider another example to check how the Array.includes method works when we have to search for the numeric values:

<script>

var items = [50, 72, 60, -14, 53, 23];

console.log("Item Found: " + items.includes(-14));

console.log("Item Found: " + items.includes(14));

console.log("Item Found: " + items.includes(23));

</script>

The output for the above code block will be like this:

14 doesn’t exist in the array, so the Array.includes() method returned false. It authenticates the working of Array.include() method.

Conclusion

Array.includes() is a predefined method in JavaScript that is used to check whether the array includes the specified item or not. The Array.includes() method returns true if the specified value exists in the array, and it returns false if the value to be searched doesn’t exist in the targeted array. It is a case-sensitive method which means the Array.includes() method consider the “array” and “Array” as two different words. This post explained what Array.includes() is? What it returns? and how to use it in JavaScript?

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.