Check If An Array Doesn’t Include A Value In JavaScript

To check if an array doesn’t include a value in JavaScript, you can use the some() method. The some() method returns true if at least one element in the array passes the test implemented by the callback function. Otherwise, it returns false.

Check If An Array Doesn’t Include A Value In JavaScript

check if an array doesn't include a value in javascript

Use some() Method To Check If Array Doesn’t Include A Value In JavaScript

In the following example, we have an array of numbers and we use the some() method to check if the array doesn’t include a value 5.

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

const notHasValue= !numbers.some(value => value === 5);

console.log(notHasValue); // true

In the above example, we use the some() method with a negation operator(!) to check if an array doesn’t include a value. If the array includes the value, the some() method returns true, and when negated, it returns false.

If you want to check if an array doesn’t include multiple values, you can use the following code snippet.

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

const notHasValues = !numbers.some(value => value === 5 || value === 10);

console.log(notHasValues); // true

As you can see from the above example, we use the logical OR operator(||) to check if an array doesn’t include multiple values.

Use includes() Method To Check If Array Doesn’t Include A Value In JavaScript

You can also use the includes() method to check if an array doesn’t include a value. The includes() method returns true if the specified element is found in the array, and false if it is not found.

Use the following code snippet to check if an array doesn’t include a value using the includes() method.

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

const notHasValue= !numbers.includes(5);

console.log(notHasValue); // true

You can also use the includes() method to check if an array doesn’t include multiple values.

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

const notHasValues= !numbers.includes(5) && !numbers.includes(10);

console.log(notHasValues); // true

As you can see from the above example, we use the logical AND operator(&&) to check if an array doesn’t include multiple values.

Use indexOf() Method To Check If Array Doesn’t Include A Value In JavaScript

Another way to check if an array doesn’t include a value is to use the indexOf() method. The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Use the following code snippet to check if an array doesn’t include a value using the indexOf() method.

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

const notHasValue = numbers.indexOf(5) === -1;

console.log(notHasValue); // true

You can also use the indexOf() method to check if an array doesn’t include multiple values.

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

const notHasValues= (numbers.indexOf(5) === -1) && (numbers.indexOf(10) === -1);

console.log(notHasValues); // true

As you can see from the above example, we use the logical AND operator(&&) to check if an array doesn’t include multiple values.

Conclusion

In this article, you have learned how to check if an array doesn’t include a value in JavaScript. There are several ways to do this, and which one you choose will depend on your preferences and the situation you are in.

I hope this article was helpful and that you now know how to check if an array doesn’t include a value in JavaScript.

Leave a Reply