01 / 21
T F

Truthy & Falsy
Values in JavaScript

Understand how JavaScript evaluates every value as true or false in boolean contexts, and avoid the common pitfalls that trip up even experienced developers.

JavaScript

What Is a Boolean Context?

JavaScript frequently needs to decide: is this value true or false? This happens whenever a value is used in a place that expects a boolean: if statements, while loops, logical operators, and ternary expressions.

if (myValue) { // This runs if myValue is "truthy" } while (items.length) { // This runs while items.length is "truthy" }

The Core Rule: Falsy Is the Short List

There are exactly 8 falsy values in JavaScript. Every other value in the entire language is truthy. Memorize the falsy list, and you automatically know the truthy side.

Falsy Values false 0 -0 0n (BigInt zero) "" (empty string) null undefined NaN Only 8 values! Truthy Values true 42, -1, 3.14 "hello", "0", "false" [] (empty array) {} (empty object) function() {} Infinity, -Infinity Everything else!

Why Truthy/Falsy Matters

Shorter Code
Instead of writing if (name !== ""), you can simply write if (name). Truthy/falsy lets you write concise conditions.
Default Values
Patterns like value || "default" rely entirely on truthy/falsy coercion to provide fallback values.
Bug Prevention
Many subtle bugs come from unexpected coercion: 0 and "" are valid data but evaluate as false.

Type Coercion Under the Hood

When JavaScript encounters a value in a boolean context, it internally calls the abstract operation ToBoolean. You can observe this behavior explicitly using the Boolean() function or the double-NOT operator !!.

Boolean("hello") // true Boolean(0) // false Boolean([]) // true (surprise!) Boolean("") // false // Double-NOT shorthand: !!"hello" // true !!0 // false !!null // false

The Falsy Eight: A Closer Look

1
false: The boolean literal.
2
0: The number zero.
3
-0: Negative zero.
4
0n: BigInt zero.
5
"": The empty string.
6
null: Intentional absence of value.
7
undefined: A variable not yet assigned.
8
NaN: Not-a-Number.

Truthy vs. true, Falsy vs. false

This is a crucial distinction. Truthy and falsy describe how a value behaves in a boolean context. They are not the same as the literal boolean values true and false.

Truthy / Falsy true / false
What is it? A behavior category Actual boolean values
Type Any type (string, number, etc.) Boolean only
Example "hello" is truthy true is true
Strict equality "hello" === true is false true === true is true

Surprisingly Truthy Values

These values look like they should be falsy, but they're actually truthy. They trip up beginners and experienced developers alike:

Boolean("0") // true — string "0", not number 0 Boolean("false") // true — the string "false"! Boolean(" ") // true — string with a space Boolean([]) // true — empty array Boolean({}) // true — empty object Boolean(new Boolean(false)) // true — Boolean wrapper object!

The Coercion Flow

Any Value Is it one of the 8 falsy values? YES NO Falsy → false Truthy → true if-block SKIPPED else-block RUNS if-block RUNS else-block SKIPPED

Logical Operators && and ||

JavaScript's || and && don't return true/false: they return one of the original operands. They use truthy/falsy to decide which one:

|| (OR)
Returns the first truthy value, or the last value if all are falsy.
&& (AND)
Returns the first falsy value, or the last value if all are truthy.
"hello" || "world" // "hello" "" || "fallback" // "fallback" 0 || null || "yes" // "yes" "hello" && "world" // "world" "" && "world" // "" 0 && "never" // 0

How to Apply This Knowledge

1
Identify the boolean context. Is the value inside an if, while, ternary, or logical operator?
2
Check: is it one of the 8 falsy values? false, 0, -0, 0n, "", null, undefined, NaN.
3
If yes, it's falsy. The condition evaluates as false. If no, it's truthy: the condition evaluates as true.
4
Watch for edge cases. Could the value legitimately be 0 or ""? If so, use strict comparison (=== null) instead of truthy/falsy.
5
Consider using ?? (nullish coalescing) when you only want to catch null/undefined, not 0 or "".

Example 1: Simple Condition

A function receives a user's name and greets them. What happens with different inputs?

function greet(name) { if (name) { return "Hello, " + name; } return "Hello, stranger!"; } greet("Alice") // "Hello, Alice" greet("") // "Hello, stranger!" greet(null) // "Hello, stranger!" greet(undefined) // "Hello, stranger!"

Example 2: Default Values with ||

Using || to provide fallback defaults is a classic JavaScript pattern:

function setPort(port) { port = port || 3000; console.log("Server on port:", port); } setPort(8080) // Server on port: 8080 setPort(undefined) // Server on port: 3000 setPort(0) // Server on port: 3000 ← BUG!

Example 3: Array & Object Gotchas

A common mistake is checking if an array or object is "empty" using a truthy check:

const items = []; const user = {}; if (items) { console.log("has items!"); // ALWAYS runs — [] is truthy! } if (user) { console.log("has user!"); // ALWAYS runs — {} is truthy! } // Correct checks: if (items.length) { ... } // 0 is falsy if (Object.keys(user).length) { ... }

Common Mistakes

✕ Treating "0" as falsy
The string "0" is truthy. Only the number 0 is falsy. Same for the string "false".
✕ Using || for defaults with 0
The pattern x || default replaces 0 and "" with the default. Use ?? instead when those values are valid.
✕ Checking arrays with if (arr)
Empty arrays [] are truthy. Check arr.length instead to test for emptiness.
✕ Comparing with == true
Writing if (x == true) triggers type coercion and gives unexpected results. Use if (x) or if (x === true).

|| vs. ??: Comparison

Expression || "default" ?? "default"
undefined "default" "default"
null "default" "default"
0 "default" 0
"" "default" ""
false "default" false
"hello" "hello" "hello"

Question 1 of 5

Which of the following values is falsy in JavaScript?

Question 2 of 5

What does the expression "" || "fallback" evaluate to?

Question 3 of 5

What is the output of Boolean([])?

Question 4 of 5

A function uses port = port || 3000. A user passes 0 as the port. What value does port become?

Question 5 of 5

Which operator should you use when you want a default value only for null and undefined, but NOT for 0 or ""?