Check if a Value is NOT equal to 0 using JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 31, 2021

banner

Photo from Unsplash

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 #

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.