Check if Array Doesn't include a Value in JavaScript


Borislav Hadzhiev

Sat Oct 16 20211 min read


Photo by Kylie Lugo

Check if Array Doesn't include a Value #

To check if an array doesn't include a value, use the logical NOT (!) operator to negate the call to the includes() method. The NOT (!) operator returns false when called on a true value and vice versa.

const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes('d'); console.log(notIncludesD); // ๐Ÿ‘‰๏ธ true const notIncludesC = !arr.includes('c'); console.log(notIncludesC); // ๐Ÿ‘‰๏ธ false if (notIncludesC) { console.log('โœ… the value c is NOT included in the array'); } else { console.log('โ›”๏ธ the value c is included in the array'); }

We use the logical NOT (!) operator to negate the calls to the Array.includes method.

This approach allows us to check if a specific value is not contained in the array.

Our first example checks if the value d is not contained in the array and returns true.

Our second example check for the value c and since it's contained in the array the return value is false.

Here are some more 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 first converts the value to a boolean and then flips the value.

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, 0 and false.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee