Skip to main content

How to Check if an Array Does Not Contain a Value in JavaScript

A fundamental task in programming is verifying the absence of an item in a list. In JavaScript, you might need to confirm a user ID is not already in an array before adding it, or check that a specific flag is not present. The modern and most readable way to do this is with the Array.prototype.includes() method.

This guide will teach you how to use includes() to check for the absence of a primitive value (like a string or number). It will also cover the correct method for checking if an array does not contain an object with a specific property value.

The Core Method for Primitives: Negating includes()

Use Case: When you need a simple true/false answer about the absence of a string, number, or other primitive value.

The Array.prototype.includes() method returns true if a value is found and false otherwise. To check if a value is not in an array, you simply negate this result with the logical NOT (!) operator.

Example of the problem:

// Problem: How do we confirm that the value 'd' is NOT in this array?
const letters = ['a', 'b', 'c'];

This is the most direct and readable modern approach to solve the problem:

const letters = ['a', 'b', 'c'];

// includes() returns false because 'd' is not in the array.
// The ! operator flips false to true.
const isNotInArray = !letters.includes('d');

console.log(isNotInArray); // Output: true

if (!letters.includes('a')) {
console.log('The value "a" is NOT in the array.');
} else {
console.log('The value "a" IS in the array.');
}
// Output: The value "a" IS in the array.
note

This pattern is intuitive and easy to understand: !array.includes(value) can be read as "not array includes value."

An Alternative for Primitives: The indexOf() Method

Before includes() was introduced, the standard way to perform this check was with Array.prototype.indexOf(). This method returns the index of a value if it's found, or -1 if it is not.

Solution: You can check if the returned value is -1 to confirm the element is not in the array.

const letters = ['a', 'b', 'c'];

// indexOf() returns -1 because 'd' is not in the array.
const isNotInArray = letters.indexOf('d') === -1;

console.log(isNotInArray); // Output: true

if (letters.indexOf('a') === -1) {
console.log('The value "a" is NOT in the array.');
} else {
console.log('The value "a" IS in the array.');
}
// Output: The value "a" IS in the array.
note

While this method works perfectly, !array.includes() is generally considered more modern and readable.

Checking for the Absence of an Object

You cannot use includes() to find an object by its property values because objects are compared by reference. To check if an array does not contain an object with a specific property, we must use an iterator method.

The cleanest way to do this is to negate the result of Array.prototype.some().

The logic:

  • array.some(obj => obj.id === 4) will return true if it finds an object with an id of 4.
  • !array.some(obj => obj.id === 4) will therefore return true only if no object with an id of 4 is found.

Solution:

const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
];

const hasNoUserWithId4 = !users.some(user => user.id === 4);
console.log(hasNoUserWithId4); // Output: true

const hasNoUserWithId2 = !users.some(user => user.id === 2);
console.log(hasNoUserWithId2); // Output: false
note

This is the most efficient and readable method because some() will stop iterating as soon as it finds a match, and the leading ! clearly communicates the intent of the check.

Conclusion

Checking for the absence of a value in a JavaScript array is a simple task with clear, modern solutions.

  • For primitive values (strings, numbers): The most readable method is !array.includes(value). The older but still functional alternative is array.indexOf(value) === -1.
  • For objects: To check that no object with a specific property exists, the best method is to negate an some() check: !array.some(obj => obj.property === value).

By choosing the correct method, you can write expressive and efficient code to validate the contents of your arrays.