How to Check if a Value is Truthy or Falsy in JavaScript
In JavaScript, every value has an inherent boolean value, meaning it can be evaluated in a boolean context (like an if statement) as either "true" or "false." Values that evaluate to true are called truthy, and values that evaluate to false are called falsy. Understanding this concept is fundamental to writing concise and effective conditional logic.
This guide will explain the difference between truthy and falsy, list all the falsy values, and teach you the standard, idiomatic ways to check for each.
The Core Concept: Truthy vs. Falsy
When a non-boolean value is used in a context that expects a boolean (such as an if statement), JavaScript performs an implicit type coercion.
- A truthy value is a value that is coerced to
true. - A falsy value is a value that is coerced to
false.
if ('hello') { // 'hello' is truthy, so it coerces to true
console.log('This will run.');
}
if (0) { // 0 is falsy, so it coerces to false
console.log('This will NOT run.');
}
The Six Falsy Values in JavaScript
The easiest way to understand this concept is to memorize the short list of falsy values. Everything else is truthy.
There are only six falsy values in JavaScript:
false0(and-0)""(an empty string)nullundefinedNaN(Not a Number)
That's it. Any value not on this list—including non-zero numbers, non-empty strings, arrays (even empty ones), and objects (even empty ones)—is truthy.
How to Check if a Value is Falsy
The standard and most readable way to check if a value is falsy is to use the logical NOT (!) operator directly in a conditional. The ! operator coerces a value to a boolean and then negates it.
The logic:
- If
myVaris falsy,!myVarwill betrue. - If
myVaris truthy,!myVarwill befalse.
Solution:
function isFalsy(value) {
if (!value) {
return true;
}
return false;
}
console.log(isFalsy(0)); // Output: true
console.log(isFalsy(null)); // Output: true
console.log(isFalsy('')); // Output: true
console.log(isFalsy('hello')); // Output: false
console.log(isFalsy(42)); // Output: false
The if (!value) check is the idiomatic way to test for any falsy value.
How to Check if a Value is Truthy
To check if a value is truthy, you simply use the value directly in a conditional statement.
The logic:
- If
myVaris truthy, theifblock will execute.
Solution:
function isTruthy(value) {
if (value) {
return true;
}
return false;
}
console.log(isTruthy('hello')); // Output: true
console.log(isTruthy(42)); // Output: true
console.log(isTruthy([])); // Output: true (see gotchas below)
console.log(isTruthy(0)); // Output: false
console.log(isTruthy(null)); // Output: false
The if (value) check is the standard way to test for any truthy value.
Common Gotchas: Empty Arrays and Objects are Truthy
This is a critical point that often confuses developers. Empty arrays ([]) and empty objects ({}) are truthy.
Example of code with problems:
const emptyArray = [];
if (emptyArray) {
console.log('This runs, because an empty array is truthy.');
}
const emptyObject = {};
if (emptyObject) {
console.log('This runs, because an empty object is truthy.');
}
If your goal is to check if an array or object has content, you must check its length or number of keys.
The Correct Way to Check for "Emptiness"
For an array: Check its length property.
const emptyArray = [];
if (emptyArray.length > 0) {
console.log('Array has items.');
} else {
console.log('Array is empty.'); // This will run
}
For an object: Check the length of the array of its keys.
const emptyObject = {};
if (Object.keys(emptyObject).length > 0) {
console.log('Object has properties.');
} else {
console.log('Object is empty.'); // This will run
}
Conclusion
Understanding truthy and falsy is essential for writing concise and correct conditional logic in JavaScript.
The key takeaways are:
- There are only six falsy values:
false,0,"",null,undefined, andNaN. Everything else is truthy. - To check if a value is falsy, use the logical NOT operator:
if (!myValue). - To check if a value is truthy, use the value directly in a conditional:
if (myValue). - Be aware that empty arrays (
[]) and empty objects ({}) are truthy. To check if they have content, you must check theirlengthorObject.keys().length.