- Courses
- 💻Programming Languages
- 🌐JavaScript
- Foundations
- Control Flow
- Truthy & Falsy Values
On this page
JavaScript Truhy and Falsy Values
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
In JavaScript logic, every single value has an inherent boolean identity. When a value is placed inside a conditional statement (like an if block), JavaScript evaluates it as either true or false. Understanding Truthy and Falsy values is a secret weapon for writing concise, professional code.
What are Falsy Values?
In JavaScript, there are only a handful of values that evaluate to false when converted to a boolean. These are known as Falsy values. If a value is not on this list, it is automatically considered Truthy.
The "Falsy Eight" list:
false– The boolean false itself.0– The number zero.0– Negative zero.0n– BigInt zero.""– An empty string (no characters, no spaces).null– The intentional absence of value.undefined– An uninitialized variable.NaN– "Not a Number" (result of a failed math operation).
What are Truthy Values?
A Truthy value is simply anything that is not in the Falsy list above. This can sometimes be counter-intuitive for beginners.
Common Truthy "Traps":
"0"– A string containing a zero (it’s not an empty string!)."false"– The word false inside a string.[]– An empty array.{}– An empty object.3.14– Any non-zero number (positive or negative).
Logical Context: How JS Uses These Values
You don't need to manually convert values to booleans; JavaScript does this automatically in boolean contexts, such as if statements or logical operators.
Code Example: Short-Circuit Logic
let username = ""; // This is Falsy
if (username) {
console.log("Welcome, " + username);
} else {
console.log("Guest Login"); // This will run because "" is falsy
}
let score = 0; // This is Falsy
if (score >= 0) {
console.log("Valid Score"); // This runs because we used a comparison operator
}
The Boolean() Function
If you ever want to explicitly check if a value is Truthy or Falsy, you can use the built-in Boolean() function or the "Double Bang" (!!) operator.
Code Example:
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // false
console.log(!![]); // true (Empty arrays are truthy!)
Summary Table: Truthy vs. Falsy Comparison
Value TypeFalsy ExampleTruthy Example****Strings"" (Empty)" " (Space), "Hi"Numbers0, NaN``1, -5, 0.1ObjectsNone{}, [] (Always Truthy)Specialsnull, undefined``Infinity
Practical Application: Developers often use Truthy/Falsy to check if data exists. For example,
if (userProfile) { ... }is a quick way to see if a profile object is loaded or if it is stillnull. Mastering this prevents "Cannot read property of undefined" errors!
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Loops
Next
Functions
- What are Falsy Values?
- What are Truthy Values?
- Logical Context: How JS Uses These Values
- The Boolean() Function
- Summary Table: Truthy vs. Falsy Comparison
