How to check if Falsy except Zero using JavaScript

avatar

Borislav Hadzhiev

Sat Oct 30 20211 min read

banner

Photo by Didssph

Check if Falsy except Zero using JavaScript #

To check if a value is falsy except zero, use the logical or operator and check if the value is truthy or the value is zero, e.g. value || value === 0. If neither condition is met, the value is falsy except zero.

index.js
const value = 0; if (value || value === 0) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… value is truthy or 0'); } else { console.log('โ›”๏ธ value is falsy'); }

We used the logical OR (||) operator to chain multiple conditions.

If either condition returns a truthy value, our if block will run.

The first condition checks if the value is truthy and the second - if the value is equal to 0.

If either condition is met, then we have a truthy value or 0.

If neither condition is met, we have a falsy value, that is not 0, and the else block runs.

The falsy values in JavaScript are: null, undefined, false, 0, NaN (not a number), "" (empty string).

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