JavaScript Truthy and Falsy: A Deep Dive
Grasp JavaScript's type coercion with practical examples and avoid common pitfalls
Working with JavaScript’s truthy and falsy values used to trip me up. Sure, I knew the basics - false
is falsy, true
is truthy. But the edge cases? Those were tricky. Let’s dive into what I’ve learned about this fundamental yet often misunderstood concept.
The Eight Falsy Values
In JavaScript, there are exactly eight falsy values. Everything else is truthy. Here they are:
false // The boolean false
0 // The number zero
-0 // Negative zero
0n // BigInt zero
"" // Empty string
null // Absence of any value
undefined // Uninitialized value
NaN // Not a Number
The Edge Cases That Break Your Brain
Type coercion in JavaScript creates some truly mind-bending scenarios.