Borislav Hadzhiev
Thu Oct 21 2021·3 min read
Photo by Jireh Foo
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.
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.
includes
method is not supported in Internet Explorer. If you have to support the browser, use the indexOf
method instead.// 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.
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.To check if a variable is equal to all of multiple values:
every()
method to iterate over the
array.every
method returns true
, if all conditions are met, otherwise
false
is returned.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.
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.
All of these equality checks have to return true
, for our if
block to run.