Skip to main content

How to Check if a Variable is Equal to One or More Values in JavaScript

A common task in programming is to check if a variable's value matches one of several possible options. For example, you might need to check if a status variable is equal to "pending", "processing", or "shipped". The modern and most readable way to handle this is by using the Array.prototype.includes() method.

This guide will teach you the best methods for checking if a variable is equal to one of, all of, or none of multiple other values, focusing on clean, modern, and readable solutions.

To Check if a Variable is Equal to One of Multiple Values

Use Case: When you need to see if a variable matches at least one value from a list of possibilities.

The most intuitive and readable way to do this is to put your possible values into an array and use the includes() method.

// Problem: How can we check if `status` is one of the valid active statuses?
const status = 'pending';
const activeStatuses = ['pending', 'processing', 'shipped'];

This method returns true if the value is found in the array, and false otherwise. It's clean and self-explanatory.

const status = 'pending';
const activeStatuses = ['pending', 'processing', 'shipped'];

if (activeStatuses.includes(status)) {
console.log('The status is active.');
} else {
console.log('The status is not active.');
}
// Output: The status is active.

The Old Method: || (Logical OR)

Before includes() was common, this was done with a chain of || operators. While functional, it is less readable and harder to maintain as the list of values grows.

const status = 'pending';
if (status === 'pending' || status === 'processing' || status === 'shipped') {
console.log('The status is active.');
}

To Check if a Variable is Equal to All of Multiple Values

Use Case: A less common but still useful check is to verify that several different variables all hold the same value.

The best method for this is Array.prototype.every(). This method checks if all elements in an array pass a test.

// Problem: How do we confirm that all three values are identical?
const val1 = 'apple';
const val2 = 'apple';
const val3 = 'banana'; // One is different

Solution: Array.prototype.every()

We can put all the values into an array and then check if every element in that array is equal to the first one.

function allAreEqual(array) {
// The .every() method will short-circuit and return false
// as soon as it finds an element that doesn't match.
return array.every(element => element === array[0]);
}

// Example Usage:
console.log(allAreEqual(['apple', 'apple', 'apple'])); // Output: true
console.log(allAreEqual(['apple', 'apple', 'banana'])); // Output: false
note

A clever alternative for this specific problem is to use a Set. new Set(['a', 'a', 'b']).size would be 2. If the size is 1, all elements are identical.

To Check if a Variable is Not Equal to Any of Multiple Values

Use Case: When you need to ensure a variable's value does not match any value in a "blacklist" or a set of invalid options.

The cleanest way to do this is to negate the result of the includes() method.

// Problem: How do we check if `username` is not a forbidden name?
const username = 'admin';
const forbiddenNames = ['root', 'admin', 'superuser'];

Solution: Negating Array.prototype.includes()

The logical NOT (!) operator flips the boolean result of includes().

const username = 'testuser';
const forbiddenNames = ['root', 'admin', 'superuser'];

// includes() returns false, which `!` flips to true.
if (!forbiddenNames.includes(username)) {
console.log('Username is valid.');
} else {
console.log('Username is forbidden.');
}
// Output: Username is valid.
note

This pattern, !array.includes(value), can be read as "if the array does not include the value," making it very intuitive.

Conclusion

JavaScript's modern array methods provide clear and expressive ways to compare a variable against multiple values.

  • To check if a variable is equal to one of several values, the best method is array.includes(variable).
  • To check if a variable is equal to all of several values (or if all values in a list are the same), use array.every(el => el === array[0]).
  • To check if a variable is not equal to any of several values, use the negated includes pattern: !array.includes(variable).

These methods are far superior to older approaches like long chains of && or || operators, as they are more readable, maintainable, and scalable.