Check if a Value is NOT equal to 0 using JavaScript

avatar

Borislav Hadzhiev

Sun Oct 31 20211 min read

Check if a Value is NOT equal to 0 #

Use the strict inequality operator to check if a value is not equal to 0, e.g. myVar !== 0. The strict inequality operator checks if two values are not equal and returns a boolean result - true if they aren't equal, and false otherwise.

index.js
const num = 0; if (num !== 0) { console.log('โœ… value is not zero'); } else { console.log('โ›”๏ธ value is zero'); }

We used the strict inequality (!==) operator to check if a value is not equal to 0.

The operator compares the values on the left and right hand side and returns true if they are not equal.

Here are some examples:

index.js
console.log(0 !== 0); // ๐Ÿ‘‰๏ธ false console.log(0 !== '0'); // ๐Ÿ‘‰๏ธ true console.log(0 !== 200); // ๐Ÿ‘‰๏ธ true console.log(null !== null); // ๐Ÿ‘‰๏ธ false console.log(undefined !== undefined); // ๐Ÿ‘‰๏ธ false console.log(NaN !== NaN); // ๐Ÿ‘‰๏ธ true
Note that the strict inequality operator does not coerce the values to the same type, it just compares them.

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.

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