ā¤ Like
šŸ”– Save
šŸ”— Share
@thedevspaceio
@thedevspaceio

JavaScript Truthy vs. Falsy

@thedevspaceio

āœ”ļø 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

ValueDescription
falseThe boolean false value.
0The number zero.
-0Negative zero.
0nBigInt zero.
""Empty string.
nullIntentional absence of value.
undefinedVariable declared but not assigned.
NaNNot-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.

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

js
const user = { name: "Alice" };
user && user.name; // "Alice"
 
null && "hidden"; // null

! (NOT)

Converts a value to its boolean opposite.

js
!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.

js
const value = "";
const safe = value ?? "fallback"; // "" — empty string is preserved
 
const count = 0;
const safeCount = count ?? 10; // 0 — zero is preserved

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io