Check if Variable is equal to Multiple Values in JavaScript

avatar

Borislav Hadzhiev

Thu Oct 21 20213 min read

banner

Photo by Jireh Foo

Check if Variable is equal to Multiple Values #

To check if a variable is equal to one of multiple values, add the values to an array and use the includes() method on the array. The includes method returns true if the provided value is contained in the array.

index.js
const str = 'hello world'; const val1 = 'hello world'; const val2 = 'bye world'; const val3 = 'test world'; if ([val1, val2, val3].includes(str)) { console.log('✅ variable is equal to at least 1 of the values'); } else { console.log('⛔️ variable is not equal to any of the values'); }
This checks if a variable is equal to at least one of multiple values. If you need to check if a variable is equal to all of multiple values, scroll down to the next subheading.

We used the Array.includes method to check if a value is contained in an array of values.

The method returns true if the value is contained in the array and false otherwise.

The includes method is not supported in Internet Explorer. If you have to support the browser, use the indexOf method instead.
index.js
// Supported in IE const str = 'hello world'; const val1 = 'hello world'; const val2 = 'bye world'; const val3 = 'test world'; if ([val1, val2, val3].indexOf(str) !== -1) { console.log('✅ variable is equal to at least 1 of the values'); } else { console.log('⛔️ variable is not equal to any of the values'); }

We used the Array.indexOf method to check if a value is contained in an array of values.

The method returns the index of the first occurrence of the value in the array or -1 if the value is not contained in the array.

Our if condition checks if the return value from the indexOf method is not equal to -1. If the condition is met, we can conclude that the variable is equal to at least 1 of the values.

Check if Variable is equal to Multiple Values - AND operator #

To check if a variable is equal to all of multiple values:

  1. Place the values in an array and use the every() method to iterate over the array.
  2. On each iteration return the result of performing an equality check against the variable.
  3. The every method returns true, if all conditions are met, otherwise false is returned.
index.js
const str = 'hello world'; const val1 = 'hello world'; const val2 = 'hello world'; const val3 = 'hello world'; const result = [val1, val2, val3].every(value => { return value === str; }); console.log(result); // 👉️ true

The function we passed to the Array.every method gets called with each element in the array, until it returns a falsy value or iterates over the entire array.

On each iteration we compare the value against a variable and return the result.

If the comparison fails, the every method short-circuits and returns false, otherwise true is returned.

An alternative approach is to use the logical AND (&&) operator.

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true, all values are equal to the variable.

index.js
const str = 'hello world'; const val1 = 'hello world'; const val2 = 'hello world'; const val3 = 'hello world'; if (str === val1 && str === val2 && str === val3) { console.log('✅ variable is equal to all of the values'); } else { console.log('⛔️ variable is not equal to all of the values'); }

We used the logical AND (&&) operator to chain multiple equality checks.

The logical AND (&&) operator returns the value to the left if it's falsy, otherwise it returns the value to the right.

All of these equality checks have to return true, for our if block to run.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee