Prompt
Use explicit existence checks instead of truthiness checks when dealing with object properties, null/undefined values, or when falsy values are valid data.
Truthiness checks can cause bugs in several scenarios:
- Object properties containing objects/promises - These are always truthy even when you want to check existence
- Falsy but valid values - Values like
0,"", orfalsethat should be processed but fail truthiness checks - Null vs undefined distinction - When you need to handle both null and undefined consistently
Examples:
// ❌ Bad: Promise objects are always truthy
if (fileProcessors[filePath]) {
return fileProcessors[filePath]
}
// ✅ Good: Check for property existence
if (filePath in fileProcessors) {
return fileProcessors[filePath]
}
// ❌ Bad: Filters out valid falsy values like "0"
return trim ? dequote(_.trim(result)) : result
// ✅ Good: Only filter out null/undefined
return trim && result != null ? dequote(_.trim(result)) : result
// ❌ Bad: Boolean true is falsy in this context check
if (keyEntry === undefined) {
// handle undefined case
}
// ✅ Good: Explicit check avoids confusion with boolean values
if (!keyEntry) {
// This would incorrectly trigger for boolean false
}
Use in operator for object property existence, != null for null/undefined checks when falsy values are valid, and be explicit about what condition you’re actually testing for.