Truthy and Falsy Values
A boolean is a value that is either true or false. But a JavaScript condition accepts any value, not just booleans: if ("Navika"), if (0) and if ([]) are all legal. JavaScript converts the value to a boolean first. Values that become true are called truthy; values that become false are called falsy. There are exactly eight falsy values, and learning them by heart prevents a large share of beginner and interview bugs.
In One Minute
- The falsy values:
false,0,-0,0n,""(empty string),null,undefined,NaN. - Everything else is truthy, including
"0","false"," ",[],{}and every function. Boolean(value)and!!valueconvert a value to a real boolean.- Truthiness is a shortcut. When
0or""are valid values, compare explicitly instead.
Booleans
Booleans come from comparisons and logical operators, or you can write them directly:
const isPaid = false;
const balance = 120;
console.log(typeof isPaid);
console.log(balance > 100);
console.log(balance > 100 && !isPaid);boolean
true
trueName boolean variables so they read as yes-or-no questions: isPaid, hasDiscount, canRefund. Then if (isPaid) reads like English.
The Eight Falsy Values
| Value | Type | Why it is falsy |
|---|---|---|
false | boolean | It is false |
0 | number | Zero |
-0 | number | Negative zero |
0n | bigint | BigInt zero |
"" | string | The empty string (no characters) |
null | null | Deliberately empty |
undefined | undefined | Not set |
NaN | number | Not a number |
const values = [false, 0, -0, 0n, "", null, undefined, NaN];
for (const value of values) {
if (value) {
console.log(String(value), "is truthy");
} else {
console.log(String(value), "is falsy");
}
}false is falsy
0 is falsy
0 is falsy
0 is falsy
is falsy
null is falsy
undefined is falsy
NaN is falsyExplanation: each value goes through if, and every one takes the else branch. String(-0) prints as 0, String(0n) as 0, and the empty string prints as nothing before " is falsy".
Everything Else Is Truthy
These surprise people because they look empty or false:
console.log(Boolean("0"));
console.log(Boolean("false"));
console.log(Boolean(" "));
console.log(Boolean([]));
console.log(Boolean({}));
console.log(Boolean(-1));
console.log(Boolean(function () {}));true
true
true
true
true
true
trueExplanation: a string is falsy only when it has zero characters: "0", "false" and " " (one space) all have characters. Every object is truthy, and an empty array or object is still an object. To test for an empty array, check its length: if (fees.length === 0).
Converting to a Boolean
| Way | Example | Result |
|---|---|---|
Boolean(value) | Boolean("Navika") | true |
!!value (double NOT) | !!0 | false |
| Condition | if (value) | converts automatically |
!value converts and flips; !!value flips twice, leaving the plain boolean. Both forms are common; Boolean(value) reads more clearly, and it is handy with array methods: names.filter(Boolean) removes every falsy item.
const names = ["Navika", "", "Srihitha", null, "Advitha", undefined];
console.log(names.filter(Boolean));['Navika', 'Srihitha', 'Advitha']Where Truthiness Bites
Truthiness is a shortcut for "is there something here?". It goes wrong when 0 or "" are real values:
function showBalance(balance) {
if (balance) {
return `Balance: $${balance}`;
}
return "No balance on file";
}
console.log(showBalance(120));
console.log(showBalance(0));
console.log(showBalance(undefined));Balance: $120
No balance on file
No balance on fileA student who owes nothing has a balance of 0, which is a real, known value. The truthy check treats it like a missing balance. The fix is to say what you mean:
function showBalance(balance) {
if (balance === undefined || balance === null) {
return "No balance on file";
}
return `Balance: $${balance}`;
}
console.log(showBalance(0));
console.log(showBalance(undefined));Balance: $0
No balance on fileIn Practice
The Fee Tracker checks a form before saving. A name must be typed (empty means missing, so truthiness fits), but an amount of 0 must be rejected with its own message rather than treated as missing:
function validate(form) {
const name = form.name.trim();
if (!name) return "Enter the student's name.";
if (form.amount === "") return "Enter an amount.";
const amount = Number(form.amount);
if (Number.isNaN(amount)) return "The amount must be a number.";
if (amount === 0) return "The amount can't be zero.";
return `Saved $${amount} for ${name}.`;
}
console.log(validate({ name: " ", amount: "50" }));
console.log(validate({ name: "Advitha", amount: "" }));
console.log(validate({ name: "Advitha", amount: "0" }));
console.log(validate({ name: "Advitha", amount: "50" }));Enter the student's name.
Enter an amount.
The amount can't be zero.
Saved $50 for Advitha.Common Mistakes
Testing an array for emptiness with if (list). Always true. Use list.length.
Treating "0" or "false" from a form as false. They are non-empty strings, so they're truthy. Convert or compare: value === "true".
Using truthiness on numbers that can be zero. Balances, counts and indexes are often 0. Compare explicitly.
Using indexOf in a condition. if (names.indexOf("Navika")) is false when Navika is at index 0, and true when she isn't found (-1). Use names.includes("Navika").
Interview Corner
What are the falsy values in JavaScript?
false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including "0", "false", [], {} and functions. (Browsers also have one legacy falsy object, document.all, kept for old pages.)
Is an empty array truthy?
Yes. All objects are truthy, regardless of contents. Check arr.length === 0 for emptiness. Note that [] == false is true because == converts the array to a number, which is a different rule from truthiness.
What does this print?
Show solution
console.log(!!"0", !!0, !![], !!"");true false true false"0" is a non-empty string (truthy), 0 is falsy, [] is an object (truthy), and "" is empty (falsy).
Practice
Challenge: Write hasItems(value) that returns true only for a non-empty array, and false for empty arrays, null, undefined and anything that isn't an array.
Show solution
function hasItems(value) {
return Array.isArray(value) && value.length > 0;
}
console.log(hasItems([120]));
console.log(hasItems([]));
console.log(hasItems(null));
console.log(hasItems("abc"));true
false
false
falseKey Takeaways
| Want to know | Write |
|---|---|
| Is there an object? | if (student) |
| Was text typed? | if (name.trim()) |
| Is a number positive? | if (amount > 0) |
| Is the list empty? | if (list.length === 0) |
| Is it missing? | if (value == null) or value === undefined |
Was this tutorial helpful?