Borislav Hadzhiev
Mon Oct 18 2021·2 min read
Photo by Anika Huizinga
Use the strict inequality (!==) operator to check if two strings are not equal
to one another, e.g. a !== b
. The strict inequality operator returns true
if
the strings are not equal and false
otherwise.
const a = 'one'; const b = 'two'; if (a !== b) { console.log('✅ strings are NOT equal'); } else { console.log('⛔️ strings are equal'); }
We used the strict inequality (!==) operator to check if two strings are not equal to one another.
The operator returns a boolean result:
true
if the values are not equalfalse
if the values are equalThe strict inequality (!==) operator is the negation of the strict equality (===) operator.
Here are some more examples.
console.log(5 !== '5'); // 👉️ true console.log('one' !== 'one'); // 👉️ false console.log('one' !== 'ONE'); // 👉️ true console.log('' !== ' '); // 👉️ true console.log(null !== null); // 👉️ false console.log(undefined !== undefined); // 👉️ false console.log(NaN !== NaN); // 👉️ true
The strict inequality (!==) operator considers two values of different types to be different, as opposed to the loose inequality (!=) operator.
This means that two values of different types will never be equal, when using the strict inequality operator.
Notice that in the last example, the evaluation NaN !== NaN
returns true
.
This is because NaN
(not a number) is the only value in JavaScript, which is
not equal to itself.