Learn what truthy and falsy values mean in JavaScript. This beginner-friendly guide explains how JavaScript decides true or false in conditions, with simple examples and tips.
Introduction
When learning JavaScript, one of the simplest yet most powerful ideas to understand is truthy and falsy values. These values help JavaScript decide what’s “true” or “false” in conditions like if
statements or loops.
Knowing how they work helps you write cleaner, smarter, and more predictable code, a must-have skill for every beginner.
What You’ll Learn
At the end of this tutorial, you’ll know:
- What truthy and falsy values are in JavaScript.
- The complete list of falsy values.
- How JavaScript decides what’s true or false in conditions.
- Common mistakes beginners make with these values.
- Real code examples to make everything crystal clear.
What Are Truthy and Falsy Values?
Every value in JavaScript is automatically considered truthy or falsy when it’s used in a condition.
That means when you write something like:
if (value) {
// do something
}
JavaScript will quietly check if value
should be treated as true or false, even if it’s not literally true
or false
.
A truthy value acts as
true
.
A falsy value acts asfalse
.
Let’s break them down and see which ones fall into each category.
List of Falsy Values in JavaScript
JavaScript has only six falsy values, and memorizing them is easy:
false
-
0
(zero) -
""
(empty string) null
undefined
-
NaN
(Not a Number)
Everything else is truthy.
Here’s how falsy values behave in code:
if (0) {
console.log("This is truthy");
} else {
console.log("This is falsy");
}
// Output: This is falsy
Because
0
is one of the falsy values, JavaScript runs theelse
block.
Now that you know falsy values, let’s see what truthy values look like.
Truthy Values in JavaScript
All values that aren’t falsy are automatically truthy.
Here are a few examples:
true
-
"Hello"
(a non-empty string) -
29
(any non-zero number) -
{}
(an empty object) -
[]
(an empty array)
Example:
if ("Hi Wisdom") {
console.log("This is truthy");
}
// Output: This is truthy
Even though
"Hi Wisdom"
isn’t a Boolean, JavaScript treats it as true in a condition.
Now, let’s compare truthy vs falsy values side by side to make it even clearer.
Truthy vs Falsy Values in Action
Here’s a simple code comparison that shows how JavaScript reacts to different values:
const values = [true, false, 0, 1, "", "JavaScript", null, undefined, [], {}, NaN];
values.forEach((value) => {
if (value) {
console.log(`${JSON.stringify(value)} is truthy`);
} else {
console.log(`${JSON.stringify(value)} is falsy`);
}
});
Output:
true is truthy
false is falsy
0 is falsy
1 is truthy
"" is falsy
"JavaScript" is truthy
null is falsy
undefined is falsy
[] is truthy
{} is truthy
NaN is falsy
This example shows exactly how JavaScript interprets different values when checking for truth or falseness.
Now that you’ve seen it in action, let’s discuss how to use this knowledge effectively in your code.
Using Truthy and Falsy Values in Conditions
When you use a value in an if
statement, JavaScript automatically coerces (or converts) it into a Boolean behind the scenes.
This process is called type coercion.
Example:
let name = "";
if (name) {
console.log("Name is provided");
} else {
console.log("Name not found");
}
// Output: Name not found
Since name
is an empty string (""
), JavaScript treats it as falsy, so the else
block runs.
This behavior helps simplify code, but it can also confuse you if you don’t fully understand it.
Common Mistakes Beginners Make
-
Assuming all values are truthy
Not all values are truthy. Remember that values like
0
,""
, andnull
are falsy. -
Confusing
false
with falsy valuesfalse
is one falsy value, but others like0
andundefined
behave the same way in conditions. Thinking empty arrays and objects are falsy
This one trips up many beginners, both []
and {}
are actually truthy!
if ([]) {
console.log("An Empty array is truthy");
}
// Output: An Empty array is truthy
}
Conclusion
Understanding truthy and falsy values is a small but powerful step in mastering JavaScript.
They control how conditions run and how your code reacts to different types of data.
Just remember this simple rule:
Only six values are falsy; everything else is truthy.
Once you know this, your code will be easier to read, debug, and predict.
It’s one of those foundational skills that every JavaScript developer should have, and now, you’ve got it.
You can reach out to me via LinkedIn
Top comments (2)
There are 7 falsy values. You've missed 0n (BigInt zero)
Thanks for pointing that out.