Home > Software > How to Check if an Array Doesn’t Include a Value in JavaScript

How to Check if an Array Doesn’t Include a Value in JavaScript

Anastasios Antoniadis

Share on X (Twitter) Share on Facebook Share on Pinterest Share on LinkedInArrays are a fundamental structure in JavaScript, used for storing lists of values. Often, while working with arrays, developers need to determine whether an array lacks a specific element. This task is common in scenarios like input validation, feature toggling, or conditionally rendering …

Javascript

Arrays are a fundamental structure in JavaScript, used for storing lists of values. Often, while working with arrays, developers need to determine whether an array lacks a specific element. This task is common in scenarios like input validation, feature toggling, or conditionally rendering UI components based on a list of available options. JavaScript offers straightforward methods to perform this check. This article delves into how to efficiently check if an array doesn’t include a particular value, showcasing various techniques and examples.

Using the includes() Method

Introduced in ECMAScript 2016 (ES6), the Array.prototype.includes() method determines whether an array includes a certain value among its entries, returning true or false accordingly. To check if an array does not include a value, you can simply negate the result of includes():

const fruits = ['apple', 'banana', 'mango', 'orange'];

// Check if 'banana' is not in the array
if (!fruits.includes('banana')) {
  console.log('Banana is not in the fruits array.');
} else {
  console.log('Banana is in the fruits array.');
}

// Check if 'grape' is not in the array
if (!fruits.includes('grape')) {
  console.log('Grape is not in the fruits array.'); // This will be executed
} else {
  console.log('Grape is in the fruits array.');
}
const fruits = ['apple', 'banana', 'mango', 'orange'];

// Check if 'banana' is not in the array
if (!fruits.includes('banana')) {
  console.log('Banana is not in the fruits array.');
} else {
  console.log('Banana is in the fruits array.');
}

// Check if 'grape' is not in the array
if (!fruits.includes('grape')) {
  console.log('Grape is not in the fruits array.'); // This will be executed
} else {
  console.log('Grape is in the fruits array.');
}

The includes() method is case-sensitive when comparing strings, and strict equality (===) for other types.

Leveraging the indexOf() Method

Before includes() was introduced, the indexOf() method was commonly used to determine if an array contained a certain value. indexOf() searches the array for the specified item and returns the first index where it was found, or -1 if the item is not present. To check for the absence of a value, you would check if indexOf() returns -1:

const numbers = [1, 2, 3, 4, 5];

// Check if 3 is not in the array
if (numbers.indexOf(3) === -1) {
  console.log('3 is not in the numbers array.');
} else {
  console.log('3 is in the numbers array.');
}

// Check if 6 is not in the array
if (numbers.indexOf(6) === -1) {
  console.log('6 is not in the numbers array.'); // This will be executed
} else {
  console.log('6 is in the numbers array.');
}

While indexOf() is widely supported across older browsers, it’s less intuitive than includes() for checking presence or absence.

Using find() for Complex Searches

For arrays containing objects, or when more complex search criteria are needed, the find() method can be useful. find() executes a callback function once for each index of the array until it finds one where the callback returns a truthy value. If such an element is found, find() immediately returns the element’s value. Otherwise, it returns undefined.

To check if an array of objects does not include an object with a certain property value, you could use find() in combination with a negation:

const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Doe' }
];

// Check if there's no user with id 4
if (!users.find(user => user.id === 4)) {
  console.log('No user with id 4.'); // This will be executed
} else {
  console.log('There is a user with id 4.');
}

Conclusion

Determining if an array does not include a particular value is a common requirement in JavaScript programming. The modern includes() method provides a straightforward, readable approach for simple presence checks. For compatibility with older JavaScript engines, indexOf() offers a reliable alternative. Meanwhile, find() caters to scenarios requiring more complex search conditions, especially in arrays of objects. Understanding these methods and knowing when to apply them allows developers to write more efficient, cleaner code when working with array data structures in JavaScript.

Anastasios Antoniadis
Follow me
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x