Last updated: Mar 3, 2024
Reading timeยท6 min
To check if a variable is equal to all of multiple values:
every()
method to iterate over the
array.every
method will return true
if the variable is equal to all of the
values.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 same approach can be used to compare multiple values.
const value1 = 10; const value2 = 10; const value3 = 10; const arr = [value1, value2, value3]; const areEqual = arr.every(element => { return element === arr[0]; }); console.log(areEqual); // ๐๏ธ 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.
every()
method short-circuits and returns false
, otherwise, it returns true
.An alternative approach is to use the logical AND (&&) operator.
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) { // ๐๏ธ this runs 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 the equality checks have to return true
for the if
block to run.
To check if a variable is equal to one of multiple values:
includes()
method on the array.includes
method will return true
if the 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.
Set
objectIf you need to check if the three values are equal, you can also pass the values to the Set() constructor.
Set
objects only store unique values, so any duplicates passed to the
constructor get automatically removed.
const set1 = new Set(['a', 'a', 'a']); console.log(set1); // ๐๏ธ {'a'}
We can pass the values to the Set()
constructor and if the size of the Set
is equal to 1
, then all of the passed-in values are equal.
const val1 = 'apple'; const val2 = 'apple'; const val3 = 'apple'; if (new Set([val1, val2, val3]).size === 1) { console.log('โ The 3 values are equal'); } else { console.log('โ๏ธ The values are NOT equal'); }
The Set
object doesn't contain duplicate values, so all of the duplicates got
removed when initializing the Set
.
If the new Set
contains a single element, then all of the passed-in values are
equal.
indexOf
This is a three-step process:
indexOf()
method on the array.-1
, the variable is equal to one of the
values.const str = 'hello world'; const val1 = 'hello world'; const val2 = 'bye world'; const val3 = 'test world'; if ([val1, val2, val3].indexOf(str) !== -1) { // ๐๏ธ this runs 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.
-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.
To check if a variable is not equal to multiple values:
const a = 'one'; const b = 'two'; const c = 'three'; if (a !== b && a !== c) { // ๐๏ธ this runs console.log('โ a is not equal to b and c'); } else { console.log('โ๏ธ a is equal to b or c, or both'); } // โ Check if a variable is not equal to multiple values (every()) const notEqual = [b, c].every(value => value !== a); console.log(notEqual); // ๐๏ธ true
We used the
strict inequality (!==)
operator to check if the variable a
is not equal to the variables b
and c
.
The operator returns a boolean result:
true
if the values are not equalfalse
if the values are equalWe used the logical AND (&&) operator to chain the two conditions.
The if
block will only run if both conditions are met.
&&
operator is evaluated from left to right. If the first condition in our if
statement returns false
, the operator short-circuits and doesn't evaluate the second condition.If the a !== b
part of the condition returns false
, the a !== c
part is
not evaluated at all.
For example, in the following if
statement, the 10 > 1
condition is never
evaluated.
if (5 > 50 && 10 > 1) { }
We first check if 5 > 50
and get a value of false
, so the &&
operator
short-circuits.
You can also use the Array.includes()
method to check if a variable does not
equal multiple values.
includes()
This is a three-step process:
Array.includes()
method to check if the variable is not contained
in the array.true
if the variable doesn't equal any of the
values.const a = 'one'; const b = 'two'; const c = 'three'; const notEqual = ![b, c].includes(a); console.log(notEqual); // ๐๏ธ true if (a !== b && a !== c) { // ๐๏ธ this runs console.log('a is not equal to b and c'); } else { console.log('a is equal to b or c, or both'); }
We wrapped the b
and c
variables into an array and used the
Array.includes()
method to check if the a
variable is contained in the
array.
The Array.includes()
method takes a value and returns true
if the value is
contained in the array and false
otherwise.
console.log([b, c].includes(a)); // ๐๏ธ false console.log(![b, c].includes(a)); // ๐๏ธ true
We used the logical NOT (!) operator to negate the call to the includes()
method.
When used with boolean values, like in the example, the operator flips the
value, e.g. true
becomes false
and vice versa.
console.log(!true); // ๐๏ธ false console.log(!false); // ๐๏ธ true
The expression returns true
if the variable doesn't equal the values and
false
otherwise.
You can also use the Array.every()
method to check if a variable does not
equal multiple values.
every()
This is a three-step process:
Array.every()
method to iterate over the array.const a = 'one'; const b = 'two'; const c = 'three'; const notEqual = [b, c].every(value => value !== a); console.log(notEqual); // ๐๏ธ true if (a !== b && a !== c) { // ๐๏ธ this runs console.log('a is not equal to b and c'); } else { console.log('a is equal to b or c, or both'); }
We wrapped the b
and c
variables into an array.
The function we passed to the Array.every()
method gets called with each
element in the array.
On each iteration, we check if the current value is not equal to the a
variable and return the result.
The every()
method checks if the condition is met for all array elements.
If it is, the method returns true
, otherwise, false
is returned.