Truthy & Falsy Values
Truthy & Falsy Values in JavaScript
In JavaScript, every single value has an inherent boolean truth value. When any non-boolean value is evaluated in a boolean context (like an if condition or logical operator), JavaScript automatically coerces it into either true (truthy) or false (falsy).
1. The Falsy Values in JavaScript
Out of the billions of values possible in JavaScript, there are only 8 falsy values. Everything else in the entire language is truthy!
+-----------------------------------------------------------------------------+ | THE 8 FALSY VALUES IN JAVASCRIPT | +-----------------------------------------------------------------------------+ | 1. false : The boolean literal false | | 2. 0 : The number zero | | 3. -0 : Negative zero | | 4. 0n : BigInt zero | | 5. "" (or '' or ``) : Empty string with zero length | | 6. null : The null primitive | | 7. undefined : The undefined primitive | | 8. NaN : "Not a Number" invalid arithmetic result | +-----------------------------------------------------------------------------+
If a value is NOT on this exact list of 8, it is 100% TRUTHY!
2. Common Truthy Surprises
Beginners frequently get tripped up by values that seem "empty" or "zero-like" in English, but are completely truthy in JavaScript:
| Value | Truthiness | Why is it Truthy? |
|---|---|---|
"0" | Truthy! | It is a string containing a character (length = 1) |
"false" | Truthy! | It is a non-empty string |
" " (whitespace) | Truthy! | Contains space character (length = 1) |
[] (Empty Array) | Truthy! | All objects and arrays in JS are truthy! |
{} (Empty Object) | Truthy! | All objects in JS are truthy! |
function(){} | Truthy! | Functions are objects, always truthy |
The Empty Array Trap:
3. Explicit Boolean Conversion
You can inspect the truthiness of any value in the console using two methods:
4. Truthy Values in Conditional Logic
Practice Quiz
[] in an if condition?Boolean("0") evaluate to?users actually has elements in it?For Loop
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Switch Statements | For Loop |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.