Loading...

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()

Falsy map with Boolean()

javascript
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.

Truthy samples beginners meet

javascript
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.

if uses truthy and falsy

javascript
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.

String traps that look falsy

javascript
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.

Empty string vs one space

javascript
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.

Page gate with truthy / falsy

html
<!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

  1. Log Boolean("0") and Boolean(0) side by side.
  2. Log Boolean(" ") and Boolean("") — space vs empty.
  3. Write a tiny if (score) with score = 0, then with score = 5.
  4. In the HTML demo, set user to "", refresh, and read the new message.
  5. Change user to "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 false in a condition — learn the short list
  • Truthy: acts like true — most everyday values
  • Preview with Boolean(value) or use if (value)
  • String traps: "0" and "false" are truthy
  • Next: operators — symbols that compute and compare

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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