You will be able to explain JavaScript’s two Boolean values, predict how common values convert to true or false, and use Boolean conversion safely in conditions.
What it is
A Boolean is a primitive JavaScript value that can only be true or false. Booleans are the result of comparisons and are used to control program flow in statements such as if, while, and for.
JavaScript often needs a Boolean even when the value is not one. For example, if (username) converts username to a Boolean. This conversion is called coercion. The mental model is: “Is this value useful enough to count as true?”
The falsy values are:
false0-00n""nullundefinedNaN
Everything else is truthy, including [], {}, "0", and "false". Related terms include truthy, falsy, Boolean object, and primitive Boolean.
Why it matters
- Conditions like
if (value)depend on Boolean coercion. - Logical operators such as
&&,||, and!work with truthy and falsy values. - Boolean conversion helps normalize user input, such as checking whether a field is filled.
- Understanding falsy values prevents bugs with empty strings, zero,
null, andundefined. - Explicit Boolean values make APIs and state easier to reason about.
Syntax or steps
- Use
Boolean(value)to convert any value totrueorfalse. - Use
if (value)when you want to accept any truthy value. - Use
if (!value)when you want to catch falsy values. - Use
value === trueorvalue === falsewhen you need an exact Boolean.
Boolean(""); // false
Boolean("hello"); // true
Boolean(0); // false
Boolean(1); // true
Boolean([]); // true
Boolean({}); // true
Example
const values = [true, false, 0, 1, "", "hello", null, undefined, NaN, [], {}, "0"];
for (const value of values) {
console.log(String(value), Boolean(value));
}
const username = "";
if (username) {
console.log("Welcome back!");
} else {
console.log("Please sign in.");
}
const isAdmin = Boolean(username === "admin");
console.log(isAdmin);
Part-by-part explanation
The first loop tests many values and prints their Boolean conversion. Empty string, 0, null, undefined, and NaN become false. Non-empty strings, numbers other than zero, arrays, and objects become true.
The if (username) block checks whether username is truthy. Because username is "", it is falsy, so the else branch runs.
The expression username === "admin" already returns a Boolean, but wrapping it in Boolean() makes the intent explicit: store a true/false result.
Common mistakes
- Assuming
0or""are truthy. They are falsy. If you need to allow zero or empty strings, check explicitly, such asvalue !== undefined. - Using
new Boolean(false). That creates a Boolean object, and objects are truthy. UseBoolean(false)instead. - Confusing
==with===. Use===when you need exact Boolean comparison. - Using
!valueto mean “value is false.” It actually means “value is falsy,” which also includes0,"",null, andundefined.
When to use it
Use implicit truthiness when you only care whether a value exists or is non-empty. Use explicit Boolean conversion when you need a clean true or false result. Use strict equality when you need to know whether a value is exactly a Boolean.
| Pattern | Use when | Example |
|---|---|---|
if (value) |
You want any truthy value | if (name) { greet(); } |
Boolean(value) |
You need a true/false result | const hasName = Boolean(name); |
value === false |
You need exact Boolean false | if (enabled === false) { stop(); } |
Practice
Guided exercise: create an array containing 0, "0", "", "hello", null, and undefined. Use Boolean() to print whether each value is truthy.
Expected output: 0, "", null, and undefined are false; "0" and "hello" are true.
Challenge: write a function called isFilled(value) that returns true only when value is a non-empty string. Hint: use typeof value === "string" and value.length > 0.
Quick check
Question: What does Boolean("false") return?
Answer: true, because "false" is a non-empty string, and non-empty strings are truthy.
Summary
JavaScript Booleans are simple: only true and false are Boolean values. Many other values become Boolean through coercion, with a small set of falsy values and everything else truthy.
Use truthiness for convenience, Boolean() for explicit conversion, and strict equality when exact Boolean values matter.