Search Here

Working with Javascript Array includes() method with examples for Beginners

Working with Javascript Array includes() method with examples for Beginners

Javascript built-in array methods are very useful and easily developing applications. In this post, we’ll cover includes() method which checks whether a particular item present in the array or not by returning boolean values.

Syntax

array.includes(search_item, from_index);
  • search_item: Represents the value to be searched in the array.
  • from_index: Represents starting index to be searched from.

This method returns a boolean value true when an item exists in an array and returns false when an item does not exist in the array. And also does not modify or change current items that exists in an array. This method is useful for conditional checking. Few examples are mentioned in this post.

Table of Contents

Examples

In this section, we’ll be working on different examples related to the includes() method and verify by ourselves how this method works.
This first example contains a variable arr which is of type array it contains numbers. Using includes() the method we’ll check if the number 36 exists in an array bypassing a number 36 as the number and also enclosed inside a string.

let arr = [25, 17, 36, 72];

console.log(arr.includes(36)); //returns true
console.log(arr.includes("36")); //returns false
console.log(arr.includes(parseInt("36"))); //returns true

The outcome of the first input arr.includes(36) returns true as a number 36 exists in an array and it is of type number. But the second outcome console.log(arr.includes("36")) returns false because the input 36 is a number but we are passing it has a string which makes it string type rather than number type. Some whenever we pass any number as an argument to includes() the method we must compulsorily convert it to an integer. This we can do by using a built-in function parseInt().

The example is of checking how includes() the method handles strings, floating-point numbers. So array arr has three items first one is a string, then a number, and the last one is of type floating-point number.

let arr = ["Sumit", 12, 15.26];

console.log(arr.includes("sumit")); //returns false
console.log(arr.includes("Sumit")); //returns true
console.log(arr.includes(15.26)); //returns true
console.log(arr.includes(15.269)); //returns false

The outcome of the first console.log(arr.includes("sumit")) is false because the first letter of Sumit is capital where out input argument did not have the first letter as a capital S. This means that the method includes() performs strict type-checking on arguments passed.

The last outcome returns false because even though the number has an additional decimal digit 9 this makes it a completely different number.

In this example, we’ll look at how includes() methods behave with negative and NaN values.

let arr = [13, -23, NaN];

console.log(arr.includes(-23)); //returns true
console.log(arr.includes(NaN)); //returns true

The first index of the array arr contains a negative number -23 passing it to includes() method will return true.
This method also works on numbers such as NaN.

Using from_index argument to verify item presence

includes() method also has an optional argument from_index which represents an index of the array and specifies that the method will search given value from that index.
We have an example that consists of numbers in an array and will see its outcomes.

let arr = [13, -23, NaN]; 

console.log(arr.includes(-23, 2)); //returns false
console.log(arr.includes(NaN, -1)); //returns true

The first outcome console.log(arr.includes(-23, 2)); -23 values to be searched in an array and 2 is the method will search the given value from the second index.
So the number -23 does not exist from the second index that’s why it returns false.

In the second outcome console.log(arr.includes(NaN, -1)); the NaN the number is placed at the second index of the array and we are specifying from_index as -1 this will search the array in reverse order and return true.

Javascript Array includes() method verify wheather the values exists in array

Javascript Array includes() method to verify whether the values exist in the array

Conclusion

We have reached the conclusion of our post Working with Javascript Array includes() method with examples for Beginners. Comment if you have any doubts and if you like then help us grow by sharing this post.

Related Posts

Summary
Review Date
Reviewed Item
Working with Javascript Array includes() method with examples for Beginners
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development