- 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
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
true as a number
36 exists in an array and it is of type number. But the second outcome
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
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
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
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
This method also works on numbers such as
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
-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
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
-1 this will search the array in reverse order and return