Truthy and Falsy Values
JavaScript conditions do not require a value to be exactly true or false.
When JavaScript sees a value in a boolean context, it converts that value to a boolean.
Boolean contexts include:
ifconditionswhileconditions- ternary conditions
- logical operators like
&&,||, and!
Values that become true are called truthy.
Values that become false are called falsy.
Boolean Context Example
const username = "Asha";
if (username) {
console.log("Welcome!");
}username is a string, not a boolean.
But because "Asha" is a non-empty string, JavaScript treats it as truthy, so the block runs.
The Falsy Values
JavaScript has a small list of falsy values.
These values become false in a boolean context:
| Value | Meaning |
|---|---|
false |
The boolean false value |
0 |
Number zero |
-0 |
Negative zero |
0n |
BigInt zero |
"" |
Empty string |
null |
Intentional absence of value |
undefined |
Missing or unassigned value |
NaN |
Invalid number result |
Examples:
console.log(Boolean(false)); // false
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(0n)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN)); // falseIf a value is not falsy, it is truthy.
Truthy Values
The rule for truthy values is simple:
Everything not on the falsy list is truthy.
Some truthy values surprise beginners.
console.log(Boolean("0")); // true
console.log(Boolean("false")); // true
console.log(Boolean(" ")); // true
console.log(Boolean([])); // true
console.log(Boolean({})); // true
console.log(Boolean(-1)); // true
console.log(Boolean(function () {})); // trueWhy?
"0"is a non-empty string."false"is a non-empty string." "contains a space, so it is not empty.- Arrays and objects are objects, and objects are truthy.
-1is a non-zero number.- Functions are objects, and objects are truthy.
Testing Truthiness
Use Boolean() when you want to see how JavaScript treats a value.
const value = "Hello";
console.log(Boolean(value)); // trueYou can also use double NOT:
console.log(!!value); // trueBoolean(value) is more explicit and easier for beginners to read.
Practical Use: Checking a String
Truthy/falsy checks are useful for strings.
const username = "Asha";
if (username) {
console.log(`Welcome, ${username}`);
}This is cleaner than:
if (username !== "" && username !== null && username !== undefined) {
console.log(`Welcome, ${username}`);
}But be careful: this only works when an empty string should count as missing.
Practical Use: Checking Array Length
An array is always truthy, even when it is empty.
console.log(Boolean([])); // trueSo this is not a good way to check whether an array has items:
const items = [];
if (items) {
console.log("This runs even though the array is empty");
}Check .length instead:
const items = ["apple", "banana"];
if (items.length) {
console.log("We have items to process.");
}items.length is a number.
0is falsy.- Any positive length is truthy.
For maximum clarity, you can also write:
if (items.length > 0) {
console.log("We have items to process.");
}Practical Use: Default Values
Truthy/falsy behavior is often used with || for fallback values.
const inputName = "";
const displayName = inputName || "Guest";
console.log(displayName); // GuestThis works because "" is falsy.
However, || treats all falsy values as missing.
const quantity = 0;
const finalQuantity = quantity || 1;
console.log(finalQuantity); // 1This may be wrong if 0 is a valid quantity.
For cases where only null or undefined should use the fallback, modern JavaScript provides ??.
const quantity = 0;
const finalQuantity = quantity ?? 1;
console.log(finalQuantity); // 0You will learn more about nullish coalescing soon.
The "0" Gotcha
The string "0" is truthy.
const input = "0";
if (input) {
console.log("This runs");
}This runs because "0" contains one character.
If you need to detect the string "0", check directly:
if (input === "0") {
console.log("Input is the string zero");
}If you need a number, convert first:
const numericInput = Number(input);
if (numericInput === 0) {
console.log("Input is numeric zero");
}The Empty Array and Empty Object Gotcha
Empty arrays and empty objects are truthy.
console.log(Boolean([])); // true
console.log(Boolean({})); // trueSo this runs:
if ([]) {
console.log("Empty array is truthy");
}To check if an array is empty:
const items = [];
if (items.length === 0) {
console.log("Array is empty");
}To check if an object has no own enumerable keys:
const user = {};
if (Object.keys(user).length === 0) {
console.log("Object is empty");
}Avoid Overly Broad Checks
Truthy/falsy checks are concise, but they can hide intent.
This is broad:
if (!value) {
console.log("Missing value");
}It catches:
false0""nullundefinedNaN
If you only want to check for null or undefined, be specific:
if (value == null) {
console.log("Value is null or undefined");
}If you only want to check for an empty string:
if (value === "") {
console.log("Value is an empty string");
}If you only want to check for zero:
if (value === 0) {
console.log("Value is zero");
}Common Mistakes
Thinking "false" Is Falsy
console.log(Boolean("false")); // trueIt is a non-empty string, so it is truthy.
Thinking "0" Is Falsy
console.log(Boolean("0")); // trueIt is also a non-empty string.
Checking an Empty Array Directly
const items = [];
if (items) {
console.log("This still runs");
}Use .length when you care about array contents.
Using || When 0 Is Valid
const count = 0;
const finalCount = count || 10;
console.log(finalCount); // 10If 0 is valid, use ?? or a direct check.
Best Practices
- Memorize the falsy values.
- Remember that everything else is truthy.
- Use truthy checks for simple existence checks.
- Use direct comparisons when
0,"", orfalseare valid values. - Check array length instead of checking the array itself.
- Use
Boolean(value)when you want an explicit boolean conversion. - Be careful with fallback values using
||.
Summary
Truthy and falsy values explain how JavaScript treats non-boolean values in conditions.
Falsy values include:
false0-00n""nullundefinedNaN
Everything else is truthy.
Remember:
"0"is truthy."false"is truthy.[]is truthy.{}is truthy.0is falsy.- Empty strings are falsy.
- Truthy/falsy checks are useful, but direct comparisons are safer when exact meaning matters.
Next, you will learn about optional chaining, a modern way to safely access nested values.