Check if two Strings are NOT equal in JavaScript


Borislav Hadzhiev

Last updated: Jul 26, 2022


Photo from Unsplash

Check if two Strings are NOT equal #

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.

The operator returns a boolean result:

  • true if the values are not equal
  • false if the values are equal

The 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.

It's always recommended to use the strict operators (!==, ===) because they don't try to coerce the values to the same type before comparing them. The strict operators produce more intuitive results.

Notice that in the last example, NaN !== NaN returns true. This is because NaN (not a number) is the only value in JavaScript that is not equal to itself.

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.