Borislav Hadzhiev
Reading timeยท3 min
Photo from Unsplash
To check if a value is not in an array, use the logical NOT (!) operator to
negate a call to the includes()
method, e.g. !arr.includes('value')
.
The expression will return true
if the value is not contained in the array
and false
otherwise.
const arr = ['bobby', 'hadz', 'com']; if (!arr.includes('XYZ')) { // ๐๏ธ this runs console.log('โ value is not in array'); } else { console.log('โ๏ธ value is in array'); }
We used the logical NOT (!) operator to negate a call to the Array.includes() method to check if the specified value is not contained in the array.
The Array.includes()
method returns true
if the supplied value is contained
in the array and false
otherwise.
Since we want to check if the value is NOT contained in the array, we have to negate (!) the result.
Here are some examples of using the logical NOT (!) operator.
console.log(!true); // ๐๏ธ false console.log(!false); // ๐๏ธ true console.log(!'hello'); // ๐๏ธ false console.log(!''); // ๐๏ธ true console.log(!null); // ๐๏ธ true
You can imagine that the logical NOT (!) operator:
boolean
boolean
true
, in all other cases it returns false
.Falsy values are: null
, undefined
, ""
empty string, NaN
(Not a number),
0
and false
.
If you have to check if a value is not contained in an array often, define a reusable function.
function notInArray(arr, value) { return !arr.includes(value); } const arr = ['bobby', 'hadz', 'com']; console.log(notInArray(arr, 'bobby')); // ๐๏ธ false console.log(notInArray(arr, 'XYZ')); // ๐๏ธ true console.log(notInArray(arr, 'com')); // ๐๏ธ false if (notInArray('XYZ')) { // ๐๏ธ this runs console.log('The value is NOT in the array'); } else { console.log('The value is in the array'); }
The notInArray
function takes an array and a value as parameters and returns
true
if the value is not in the array and false
otherwise.
An alternative approach is to use the Array.indexOf method.
Use the Array.indexOf()
method to check if a value is not in an array.
If the indexOf
method returns -1
, the value is not contained in the
array.
const arr = ['bobby', 'hadz', 'com']; if (arr.indexOf('XYZ') === -1) { // ๐๏ธ this runs console.log('โ value is not in array'); } else { console.log('โ๏ธ value is in array'); }
The Array.indexOf method returns the index of the first occurrence of the supplied value in the array.
If the value is not contained in the array, the method returns -1
.
Our if
statement checks if the method returned -1
.
If it did, then the value is not contained in the array.
You can define a reusable function if you have to do this often.
function notInArray(arr, value) { return arr.indexOf(value) === -1; } const arr = ['bobby', 'hadz', 'com']; console.log(notInArray(arr, 'bobby')); // ๐๏ธ false console.log(notInArray(arr, 'XYZ')); // ๐๏ธ true console.log(notInArray(arr, 'com')); // ๐๏ธ false if (notInArray('XYZ')) { // ๐๏ธ this runs console.log('The value is NOT in the array'); } else { console.log('The value is in the array'); }
The function takes an array and a value as parameters and returns true
if the
value is not contained in the array and false
otherwise.
To check if an object is not contained in an array:
Array.every()
method to iterate over the array.every()
method will return true
if the object is not contained in the
array.const arr = [ {id: 1, name: 'Alice'}, {id: 2, name: 'Bobby'}, {id: 3, name: 'Carl'}, ]; const notInArray = arr.every(obj => { return obj.id !== 4; }); console.log(notInArray); // ๐๏ธ true if (notInArray) { // ๐๏ธ this runs console.log('The object is NOT contained in the array'); } else { console.log('The object is contained in the array'); }
The function we passed to the Array.every() method gets called with each element (object) of the array.
On each iteration, we check if the current object doesn't have an id
property
with a value of 4
.
If all invocations of the callback function return a truthy value, then the
Array.every()
method returns true
, otherwise, false
is returned.
If the callback function we passed to the Array.every()
method returns a falsy
value, then Array.every()
short-circuits also returning false
.