JavaScript includes() method; In this tutorial, you will learn how to checks whether the javascript array contains the specified element or not using the includes() method on the array instance.
JavaScript Array includes()
method
The includes()
method returns true
if an array contains a given element; Otherwise, it returns false
.
The following illustrates the syntax of the includes()
method:
array.includes(element,fromIndex);
Code language: CSS (css)
The includes()
accepts two arguments:
- The first argument is the
element
that can be searched. - The
fromIndex
is the position in the array to which the search starts.
Examples of How to Check if an Array Contains a Value in Javascript
See the following, if you are working with javascript array and want to check if the array contains an element. So, you use the js method indexOf():
let numbers = [3,4,5,6,7,8];
if(numbers.indexOf(5) !== -1){
// process here
}
The indexOf()
method returns the index of the first occurrence of the element in the array. It returns -1
, the elements does not exist in given array.
Note that, the indexOf()
method uses strict equality operator (===
) for comparison, therefore, it doesn’t work with NaN
as shown in the following example:
[NaN].indexOf(NaN); // -1
In this example, the array contains one element of NaN. However, the indexOf(NaN)
returns -1
.
The includes()
method returns true
if an javascript array contains a given element; Otherwise, it returns false
.
The following represents the syntax of the includes()
method:
array.includes(element,startIndex);
The includes()
takes 2 parameters:
- The first parameter is the
element
that can be found in the array. - The
startIndex
is the position in the array to which the search starts.
See the following example:
[1,2,3].includes(2); // true
[1,2,3].includes(4); // false
[1,2,3].includes(1,1); // false
Both methods indexOf()
method and the includes()
method works absolutely fine with the NaN
:
[NaN].includes(NaN); // true
Note that the includes()
doesn’t differentiate between +0
and -0
. See the following example:
[-0].includes(+0); // true
The following example demonstrates how to use the includes()
method to check if an object is in an array.
let php = {name: 'PHP' },
java = { name: 'JAVA'},
c = {name: 'C'}
let lang = [java, c];
console.log(lang.includes(java)); // true
console.log(lang.includes(php)); // false
In this example:
- First, we initialized the
lang
array with three objects:java
andc
. - Then, we used the
includes()
method to check if thelang
array contains thejava
object, in this case, it returns true. - Finally, the
php
object is not in thelang
array, therefore, theincludes()
method returnstrue
as expected.
Difference between Array.includes() and Array.indexOf()
The javascript method Array.includes() was introduced in ECMAScript 6. The includes() method is used to find out whether the element is included in the array or not. It returns the boolean value: true or false.
We use array .indexOf() to find out whether the element is present in the array or not. But it doesn’t return a boolean. It returns the first index of the element found in the array, or it will return -1 (which represents that the element is not found).
Unlike array indexOf(), which uses Strict Equality Comparison, it includes compares using the SameValueZero equality algorithm. What that means that you can detect if the array includes a NaN.
Conclusion
In this tutorial, you have learned how to use the JavaScript Array includes()
method to determines whether an array contains a specified element.