Check if two Numbers are NOT equal in JavaScript


Borislav Hadzhiev

Mon Oct 18 20212 min read

Check if two Numbers are NOT equal #

Use the strict inequality (!==) operator to check if two numbers are not equal to one another, e.g. a !== b. The strict inequality operator returns true if the numbers are not equal and false otherwise.

const a = 5; const b = 10; if (a !== b) { console.log('โœ… numbers are NOT equal'); } else { console.log('โ›”๏ธ numbers are equal'); }

We used the strict inequality (!==) operator to check if the numbers 5 and 10 are not equal to one another.

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(3 !== 3); // ๐Ÿ‘‰๏ธ false console.log(3 !== '3'); // ๐Ÿ‘‰๏ธ true console.log(3 !== 5); // ๐Ÿ‘‰๏ธ 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 and return more intuitive and easier to read results.

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.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee