Table of Contents #
- Check if variable is equal to ALL of multiple values
- 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:
- Place the values in an array and use the
every[]
method to iterate over the array. - Check if each value is equal to the variable.
- The
every
method will returntrue
if the variable is equal to all the values.
Copied!
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.
Copied!
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:
- Add the values to an array.
- Call the
includes[]
method on the array. - The
includes
method will returntrue
if the value is contained in the array.
Copied!
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
.
Copied!
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 #
- Check if Multiple Values exist in Array in JavaScript
- Check if String starts with one of Multiple Values in JS