JavaScript Truthy and Falsy
You can convert with String(), Number(), and Boolean(). The next skill is reading how values behave inside a decision — even before the full if / else chapter. Values that act like “no” are called falsy. Almost everything else is truthy. Learn the short falsy list once; it saves hours of confusion later.
What are truthy and falsy in JavaScript?
📊 Falsy values beginners must know
| Value | Why it feels “empty / no” | Note |
|---|---|---|
false |
the boolean no | keyword |
0 |
numeric zero | also -0 |
"" |
empty string | length 0 |
null |
intentional empty | from earlier session |
undefined |
missing value | from earlier session |
NaN |
failed number | still typeof "number" |
That is the whole starter list. Memorize falsy, not an endless truthy catalog. If a value is not on this list, treat it as truthy until a later lesson says otherwise.
See falsy with Boolean()
console.log(Boolean(false));
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean(NaN));
Truthy is the default for beginners
Non-empty strings, most numbers, and true itself are truthy. The surprises that bite first are strings that look like “no” but still have characters.
console.log(Boolean(1));
console.log(Boolean(-3));
console.log(Boolean("hi"));
console.log(Boolean("0"));
console.log(Boolean("false"));
console.log(Boolean(" "));
"0" and "false" are truthy because they are non-empty text. A single space " " is also truthy — only "" (length zero) is falsy among ordinary strings.
if without calling Boolean()
A simple if (value) uses the same rule. You do not have to write if (Boolean(value)). Full branching style comes later; here you only need the yes/no gate.
let name = "Ada";
if (name) {
console.log("name is truthy");
}
let empty = "";
if (empty) {
console.log("will not run");
} else {
console.log("empty string is falsy");
}
String traps: "0" and "false"
Form fields and APIs often send text. The string "0" is not the number 0. The string "false" is not the boolean false. Both are truthy.
console.log(Boolean("0"));
console.log(Boolean(0));
console.log(Boolean("false"));
console.log(Boolean(false));
console.log("0" === 0);
Empty string vs one space
Only length-zero "" is falsy among ordinary strings. A space still counts as content.
console.log(Boolean(""));
console.log(Boolean(" "));
console.log("".length);
console.log(" ".length);
Gate a message on the page
Use one value to decide what the visitor sees. Change the value and refresh.
<!DOCTYPE html>
<html>
<head>
<title>Truthy falsy — page gate</title>
</head>
<body>
<p id="msg">…</p>
<script>
let user = "Sam";
let box = document.getElementById("msg");
if (user) {
box.textContent = "Hello, " + user;
} else {
box.textContent = "Please sign in";
}
</script>
</body>
</html>
Set user to "" or null, refresh, and the message flips to the sign-in line. Same falsy rule — now on the page.
Try it yourself
- Log
Boolean("0")andBoolean(0)side by side. - Log
Boolean(" ")andBoolean("")— space vs empty. - Write a tiny
if (score)withscore = 0, then withscore = 5. - In the HTML demo, set
userto"", refresh, and read the new message. - Change
userto"0"and notice the greeting still runs (truthy string).
Common mistakes
- Treating
"false"or"0"as falsy — they are non-empty strings - Thinking you must always wrap
Boolean()—if (value)already coerces - Trying to memorize every truthy value instead of the short falsy list
- Confusing number
0(falsy) with string"0"(truthy)
Summary
- Falsy: acts like
falsein a condition — learn the short list - Truthy: acts like
true— most everyday values - Preview with
Boolean(value)or useif (value) - String traps:
"0"and"false"are truthy - Next: operators — symbols that compute and compare
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top