To check if an element is not included in an array in JavaScript, you can use the Array.prototype.includes()
method along with the !
operator. This method returns a Boolean value indicating whether the specified element is found in the array. Here is an example:
const numbers = [1, 2, 3, 4, 5];
if (!numbers.includes(6)) {
console.log('6 is not in the array');
}
Code language: JavaScript (javascript)
In this code, we first define an array called numbers
that contains the numbers 1 through 5. Then, we use the Array.prototype.includes()
method along with the !
operator to check if 6
is not in the numbers
array. Since is not found, the code will print a message to the console.
Using Array.prototype.find()
If you have some more advanced logic required then you can use Array.prototype.find
because you can pass in a function for determining if a value matches your given criteria, rather than just checking if a value doesn’t exist.
For example:
// Define an array of objects representing a list of employees
const employees = [
{ id: 1, name: "Alice", department: "Sales" },
{ id: 2, name: "Bob", department: "Marketing" },
{ id: 3, name: "Carol", department: "Sales" }
];
// Check if the employee with the specified ID is not in the array of employees
if (!employees.find(employee => {
return employee.id === 4 && employee.department === "Sales";
})) {
console.log(`Employee with ID: 4 and Department: Sales was not found`);
}
Code language: JavaScript (javascript)
The callback function for the find()
method checks if the employee has the specified ID and is in the “Sales” department. If the find()
method returns undefined
, it means that the employee was not found in the array, and the code will print a message to the console.
Using indexOf()
Another, sort of outdated, way to check if a value is not in an array in JavaScript is to use the Array.prototype.indexOf()
method. This method returns the index of the specified element in the array, or -1 if the element is not found. Here is an example:
const numbers = [1, 2, 3, 4, 5];
if (numbers.indexOf(6) === -1) {
console.log('6 is not in the array');
}
Code language: JavaScript (javascript)
To check if a value is in an array using indexOf
then you would check that the value returned by indexOf
is greater than -1
:
const numbers = [1, 2, 3, 4, 5];
if (numbers.indexOf(5) > -1) {
console.log('5 is in the array');
}
Code language: JavaScript (javascript)
Or you can check for greater or equal to 0. But make sure to use >=
and not >
!
if (numbers.indexOf(1) >= 0) {
console.log('1 is in the array');
}
Code language: JavaScript (javascript)
What Not to Do
These methods also work, but shouldn’t be used because the above methods are more concise and performant.
Using Array.protoype.every()
The Array.prototype.every()
method returns a boolean indicating whether all elements in the array satisfy a given condition.
if (!myArray.every(val => val !== myValue)) {
// myValue is not in myArray
}
Code language: JavaScript (javascript)
Using Array.protoype.filter()
Array.prototype.filter
creates a new array with all elements that pass a given condition. If the resulting array is empty, it means that the value is not present in the original array.
if (myArray.filter(val => val === myValue).length === 0) {
// myValue is not in myArray
}
Code language: JavaScript (javascript)
Leave a Reply