Many methods of the Array objects can be used to check if an array doesn’t include a value in JavaScript. Learn more about them with examples below.
Check If Array Doesn’t Include A Value In JavaScript
With Array.includes()
The includes() method of an Array object can determine whether a specific value is included in that array with this syntax:
includes(value, index)
Parameters:
- 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.
With a positive value, includes() will search from the element with that index. Otherwise, the search will begin at arr.length + index with negative values. When omitted, JavaScript will check for every element in the array, meaning its default value is 0.
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'));
Output:
true
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));
Output:
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));
Output:
true
true
With Array.indexOf()
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():
indexOf(value, index)
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));
Output:
0
3
-1
-1
3
3
-1
4
As you can notice, all the false values have been replaced by -1, while indexOf() returns an index instead of true. Note that JavaScript uses zero-based indexing.
With Array.some()
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.
Example:
const sites = ['ITTutoria', 'Reddit', 'Stack Overflow', 'Quora'];
console.log(sites.some(arr => arr === 'ITTutoria'));
console.log(sites.some(arr => arr === 'Medium'));
Output:
true
false
The above examples use arrow functions to specify test functions (comparison operation).
Summary
You can use the includes()
, indexOf()
, and some()
methods to check if an array doesn’t include a value in JavaScript. While the first two are designed for this specific purpose, some() is more versatile and can be used in other cases as well.
Reference Sources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some