Check if a Value is NOT in an Array using JavaScript

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Table of Contents #

  1. Check if a Value is NOT in an Array in JavaScript
  2. Check if a Value is NOT in an Array using indexOf
  3. Check if an Object is NOT contained in an array in JavaScript

Check if a Value is NOT in an Array in JavaScript #

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.

index.js
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.

index.js
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:

  1. converts the value to a boolean
  2. flips the boolean
When you negate a falsy value, the operator returns 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.

index.js
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.

Check if a Value is NOT in an Array using indexOf #

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.

index.js
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.

index.js
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.

Check if an Object is NOT contained in an array in JavaScript #

To check if an object is not contained in an array:

  1. Use the Array.every() method to iterate over the array.
  2. Check if each object doesn't have a property equal to the specified value.
  3. The every() method will return true if the object is not contained in the array.
index.js
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev