Explain Codes LogoExplain Codes Logo

In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?

javascript
type-coercion
equality-checks
truthiness
Nikita BarsukovbyNikita Barsukov·Feb 23, 2025
TLDR

In JavaScript, "0" is falsy in a loose comparison (==), but when evaluated by an if statement, it is considered truthy. Here’s a bite-sized code demo:

if ("0") console.log("Can't handle the truthines!"); // "0" is truthy Boolean("0"); // true, because "0", despite seeming falsy is a total truth diva! "0" == false; // also true, type coercion is the magician here

Key insight: Falsy values like "0" or 0 aren’t strictly false, but can masquerade as false due to type coercion. In if statements, they reveal their truthiness or falsiness strut.

Falsely accused: Coercion in comparisons

JavaScript's type coercion causes seemingly odd behaviors during comparisons. With a loose equality (==), JavaScript tries to convert both operands into a common comparable type. The specifics:

  • For “0” == false, JavaScript bakers knead both operands into Number dough: 0.
  • On the other hand, an if statement tastes the flavor of truthiness or falsiness in a condition with abstract operation ToBoolean. Using this secret recipe, all non-empty strings including "0", become tastefully truthy.

This behavior might initially seem like JS having a mood swing, but it's all about understanding the internal processes that trigger these different reactions.

Rules of the jungle: Understanding specifications

The ECMAScript Language Specification is the treasure map to solve the mysteries of JavaScript. It explains how the GetValue operation influences ==, which enlightens us about the secret rituals performed during type coercion.

Dorey's GitHub repository and the JavaScript Equality Table are the "X-marks-the-spot" on this treasure map—they offer visual aids to predict outcomes of equality checks. They're the Google Maps kind of cheat sheet for JavaScript's sometimes byzantine behaviors.

Curious case of comparison in JavaScript

Here's a quick tour of examples to navigate JavaScript's comparison wonderland:

console.log("0" == false); // true, "0" decided to costume as 0 console.log("0" === false); // false, Halloween is over in strict comparison city console.log(0 == false); // true, friendly get-together in the 0 club console.log("0" == null); // false, null's invitation only covers undefined

The tour guide's advice: embrace strict comparisons (===) to avoid unexpected turns and enjoy the JavaScript journey.

Toolbox for a pragmatic developer

To wield this knowledge like a pro, make these JavaScript truths your daily coding mantras:

  • Put === to Work: Use strict equality checks (===) when you're looking for constancy. It helps shield code from unexpected outcomes due to type coercion.
  • Convert Like a Pro: To treat "0" as falsy, perform an explicit conversion to a number before making a comparison. Number("0") or +"0"` are your magic spells.
  • Table is Set: Bookmark the JavaScript Equality Table or any other visually rich resource illustrating JavaScript's equality and truthiness.
  • Log, Test, Repeat: Use console.log() to investigate the evaluation of conditions and comparisons. Like a detective questioning suspects!

Adopt these habits to dodge truthiness and falsiness traps and shine bright in the JavaScript jungle.