How many ways to check if an array doesn’t include a value in JavaScript? Here are a few ways to answer this question.
Check If Array Doesn’t Include A Value In JavaScript
The purpose of that check is to determine the information in the array from which we can easily change the values of the elements in the array.
Using includes()
The includes() method is used to determine if there are any elements in the array.
Syntax:
includes(searchElement, Index);
Parameters:
- searchElement: the value you want to look for in the array.
- Index: An optional parameter that represents where to start the search. You can use a positive or negative value for this parameter.
Note: the include()
is case-sensitive.
When there is a positive value the includes()
method will search from the Index element. For negative numbers, includes()
will start searching from the array’s length + Index. The default value is 0 (regardless of sign). If searchElement is found, it will return true. Otherwise, if it is not found, it will return false.
EXAMPLE 1:
const arr1 = [4, 7, 9]; console.log(arr1.includes(4));
Output:
true
The method returns true because the number 4 is an element in the arr1 array.
EXAMPLE 2:
const arr2 = ['sky', 'tree', 'manman']; console.log(arr2.includes('sky'));
Output:
true
The method returns true because ‘sky’ string is an element in the arr2 array.
If Index is greater than or equal to the length of the array, false is returned.
EXAMPLE 3:
const arr = ['t', 'h', 'j']; console.log(arr.includes('h', 10)) ;
Output:
false
If Index is negative, the entire array will be searched.
EXAMPLE 4:
const arr = ['t', 'h', 'j']; console.log(arr.includes('h', -10)) ;
Output:
true
The method is true because:
- array length is 3.
- Index is -10.
- computed Index is -7.
This method is also used to search for NaN or undefined values:
const arr1 = [4, 8, 4, 12 / 'T', undefined]; console.log(arr1.includes(12 / 'T')); console.log(arr1.includes(undefined));
Output:
true
true
Using indexOf()
The indexOf()
method returns the first Index at which a given element can be found in the array, or -1 if it is not present.
Syntax:
indexOf(searchElement,Index)
Parameters:
- searchElement: the value you want to locate in the array.
- Index: the optional parameter. If the Index is greater than or equal to to the array’s length, which means the array will not be founded. If Index is a negative number, it is taken as the offset from the end of the array.
- Note: the
indexOf()
method uses strict equality( triple-equals operator).
EXAMPLE:
const arr1 = ['learnshareit', 'English', 'hard']; console.log(arr1.indexOf('learnshareit')); const arr2 = [3,8,8]; console.log(arr2.indexOf(3)); console.log(arr2.indexOf(7)); console.log(arr2.indexOf(8,2));
Output:
0
0
-1
2
Using some()
The some()
method checks whether there is at least one element in the array that passes requests of the provided function. In the array, it returns true if it finds an element that passes demands of the provided function; otherwise, it returns false.
Syntax:
arr.every(callback(element[, index[, array]])[, thisArg])
Parameters:
- callback: This argument stores the function that will be invoked for each array element.
- element: The parameter’s value represents the elements that are currently being processed.
- index: This optional parameter contains the array’s currentValue element’s index, starting at 0.
- array: This optional argument holds the entire array on which Array.every is called.
- thisArg: This optional parameter contains the context that will be utilized when the callback function is executed. Every time the callback function is called, the context will be used if it is supplied. Otherwise, undefined is used by default.
Example:
const arr = ['learnshareit', 'English', 'Stack Overflow', 'pass']; console.log(arr.some(arr => arr === 'learnshareit')); console.log(arr.some(arr => arr === 'English'));
Output:
true
true
Note: if array is empty when calling this method will return false for any condition.
Summary:
Here are some ways to check if an array doesn’t include a value in JavaScript. You can use the includes()
, indexOf()
and some()
methods depends on specific requirements.
Maybe you are interested:
- Add a Key/Value pair to all Objects in Array in JavaScript
- Count Occurrences of each Element in Array in JavaScript
- Remove Duplicates from an Array in JavaScript

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java