True or False
JavaScript Booleans
Booleans have only two values — `true` and `false`. But every value in JavaScript is "truthy" or "falsy" when used in a condition.
What you'll learn
- Write boolean literals
- Recognize the eight falsy values
- Convert any value to a boolean
A boolean is either true or false. That’s the whole type.
const isActive = true;
const isLoggedIn = false;
const isAdult = 18 >= 18; // comparison produces a boolean
console.log(isActive, isLoggedIn, isAdult); Comparison operators (===, >, <, etc.) and logical operators
(!, &&, ||) produce booleans.
Truthy and Falsy
In an if condition, JavaScript converts whatever value you give it
to a boolean. Most values become true — but a handful become
false. Those are called falsy.
The complete list of falsy values:
| Value | Why it’s falsy |
|---|---|
false | obviously |
0 | zero |
-0 | negative zero |
0n | bigint zero |
"" | empty string |
null | ”deliberately empty” |
undefined | ”not assigned” |
NaN | not a number |
Everything else is truthy. Including:
"false"(string containing the word — truthy because it’s a non-empty string)"0"(same — non-empty string)[](empty array — still an object){}(empty object)Infinity
if (0) console.log("0 truthy"); // not logged
if ("") console.log("'' truthy"); // not logged
if (null) console.log("null truthy"); // not logged
if ([]) console.log("[] truthy"); // ← logged! arrays are objects
if ({}) console.log("{} truthy"); // ← logged! objects are truthy
if ("0") console.log("'0' truthy"); // ← logged! non-empty string Converting To a Boolean
Two ways:
console.log(Boolean("")); // false
console.log(Boolean("hello")); // true
console.log(Boolean(0)); // false
console.log(Boolean(42)); // true
console.log(Boolean([])); // true ← still truthy
console.log(Boolean(null)); // false
// Double-bang — same as Boolean():
console.log(!!"hello"); // true
console.log(!!""); // false The ! operator turns truthy → false and falsy → true. Apply it
twice and you get a real boolean.
Common Patterns
Default value with ||
The || operator returns the first truthy operand:
const name = "" || "Anonymous";
console.log(name); // "Anonymous" ← empty string is falsy
const count = 0 || 5;
console.log(count); // 5 ← but 0 is also falsy, which may be a bug The count example shows the pitfall: || treats 0, "", and
false as “missing”. When you only want to substitute on null/
undefined, use ?? (nullish coalescing) — covered in the Control
Flow chapter.
Boolean from condition
When you only care about a yes/no answer:
const items = [1, 2, 3];
const hasItems = !!items.length; // true
console.log(hasItems);
const hasName = Boolean("Ada"); // true
console.log(hasName); Up Next
You can store single values. Now: storing collections of them.
JavaScript Arrays →