JavaScript Truthy vs. Falsy
āļø Truthy vs Falsy Values in JavaScript
Every value in JavaScript is either truthy or falsy. Knowing which values are falsy helps you avoid bugs and write cleaner code.
ā
Falsy values
ā
Truthy values
ā
Logical OR (||)
ā
Logical AND (&&)
ā
Logical NOT (!)
ā
Nullish coalescing (??)
#javascript #truthy #falsy #logicaloperators #nullishcoalescing #typecoercion #conditionals #webdev
Falsy values
| Value | Description |
|---|---|
false | The boolean false value. |
0 | The number zero. |
-0 | Negative zero. |
0n | BigInt zero. |
"" | Empty string. |
null | Intentional absence of value. |
undefined | Variable declared but not assigned. |
NaN | Not-a-Number. |
JavaScript values are either truthy or falsy. All other values are truthy, such as non-empty strings, non-zero numbers, objects, arrays, and functions.
Empty arrays and objects are also truthy.
Logical operators
|| (OR)
Returns the first truthy value or the last value if all are falsy.
const value = "";
const safe = value || "fallback"; // "fallback"
const count = 0;
const safeCount = count || 10; // 10 ā beware: 0 is falsy&& (AND)
Returns the first falsy value or the last value if all are truthy.
const user = { name: "Alice" };
user && user.name; // "Alice"
null && "hidden"; // null! (NOT)
Converts a value to its boolean opposite.
!true; // false
!""; // true
!!"hello"; // true (double negation for boolean coercion)Nullish coalescing
??
Returns the right-hand value only if the left-hand value is null or undefined.
const value = "";
const safe = value ?? "fallback"; // "" ā empty string is preserved
const count = 0;
const safeCount = count ?? 10; // 0 ā zero is preservedFull-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.