Check if Variable is equal to Multiple Values in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Check if variable is equal to ALL of multiple values
  2. Check if variable is equal to ONE of multiple values

Check if Variable is equal to Multiple Values #

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. Check if each value is equal to the variable.
  3. The every method will return true if the variable is equal to all the values.
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.

We compare each value against the variable and return the result.

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

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 the equality checks have to return true for the if block to run.

Check if Variable is equal to One of Multiple Values #

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

  1. Add the values to an array.
  2. Call the includes() method on the array.
  3. The includes method will return true if the 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'); }

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.

You can also use the indexOf method instead of includes.

index.js
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.

The if statement checks if the return value of the indexOf method is not equal to -1. If the condition is met, the variable is equal to at least 1 of the values.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.