The includes() method of an Array object can determine whether a specific value is included in that array with this syntax:
- value: the element you want to look for in the array.
- index: the optional parameter that specifies where the search should begin. You can use positive or negative indexes for this parameter.
The includes() method only returns boolean values. If the value is found in the array or the portion set by the index parameter, it returns true, and false otherwise.
Keep in mind that this method is case-sensitive when it comes to characters and strings, while zero signs don’t matter with zero values.
This simple example demonstrates how you can use includes() to search for a string element in an array:
const sites = ['ITTutoria', 'Reddit', 'Stack Overflow', 'Quora']; console.log(sites.includes('ITTutoria'));
The method returns true because the ‘ITTutoria’ string is indeed an element in the sites array.
You can also search includes() to find non-string elements:
const birthyears = [1989, 1992, 1996, 1997, 2000, 2004, 2005, 2007]; console.log(birthyears.includes(1997)); console.log(birthyears.includes(1997, 4)); console.log(birthyears.includes(1997, 8)); console.log(birthyears.includes(1997, -5)); console.log(birthyears.includes(1997, -9));
true false false true true
It is easy to understand why the first call of includes() returns true. The second one shows the use of the index parameter. While 1997 is present in the array, false is returned because this value isn’t included after the 3rd element (index 4). If you want to sort the array before searching, check out this guide.
When you provide an index larger than the array’s length, includes() always returns false. The final two lines show the uses of negative indexes. Note that when their absolute value is greater than the array’s length, includes() will search the entire array.
You can also use this method to search for NaN and undefined values:
const arr = [1, 2, 3, NaN, undefined]; console.log(arr.includes(NaN)); console.log(arr.includes(undefined));
While includes() can indicate whether a value is available in an array, it doesn’t produce any meaningful information about the location of that value. You can use the indexOf() method instead when you need this index.
The syntax and parameters of this method are similar to those of includes():
The primary difference is the returned value of indexOf() is the index of the first element of the value. If none is found, it returns -1. It is important to note that this method uses strict equality, like the triple-equals operator ===, to conduct the search.
The following examples show you indexOf() behaves differently from includes():
const sites = ['ITTutoria', 'Reddit', 'Stack Overflow', 'Quora']; console.log(sites.indexOf('ITTutoria')); const birthyears = [1989, 1992, 1996, 1997, 2000, 2004, 2005, 2007]; console.log(birthyears.indexOf(1997)); console.log(birthyears.indexOf(1997, 4)); console.log(birthyears.indexOf(1997, 8)); console.log(birthyears.indexOf(1997, -5)); console.log(birthyears.indexOf(1997, -9)); const arr = [1, 2, 3, NaN, undefined]; console.log(arr.indexOf(NaN)); console.log(arr.indexOf(undefined));
0 3 -1 -1 3 3 -1 4
The some() method checks whether there is any element in an array able to make a specific function return true. You can also use it to find a value in an array with an appropriate function.
const sites = ['ITTutoria', 'Reddit', 'Stack Overflow', 'Quora']; console.log(sites.some(arr => arr === 'ITTutoria')); console.log(sites.some(arr => arr === 'Medium'));
The above examples use arrow functions to specify test functions (comparison operation).
You can use the