Beginner LevelJavaScript & TypeScript
What are truthy and falsy values in JavaScript?
❓ Interview Prompt: List all 8 falsy values in JavaScript and explain how boolean coercion works in conditionals.
In JavaScript, a value is considered truthy or falsy when evaluated in a boolean context (like an if statement).
Exactly 8 Falsy Values in JavaScript:
1
false2
0 (and -0, 0n BigInt zero)3
"" (empty string)4
null5
undefined6
NaN7
document.all (browser legacy quirk)EVERYTHING ELSE IS TRUTHY, including:
•
Empty arrays
[]•
Empty objects
{}•
String
"false" or "0"•
Functions
Code Implementation Example
javascript
// All evaluate to false in boolean context
if (!0) console.log('0 is falsy');
if (!"") console.log('empty string is falsy');
if (!null) console.log('null is falsy');
if (!undefined) console.log('undefined is falsy');
if (!NaN) console.log('NaN is falsy');
// SURPRISE TRUTHY VALUES:
if ([]) console.log('Empty array is TRUTHY!');
if ({}) console.log('Empty object is TRUTHY!');
if ("0") console.log('String "0" is TRUTHY!');
// Boolean coercion shorthand
const isNonEmpty = !!items.length; // Converts length (number) to boolean💡Exhaustive list of falsy values and common truthy surprises like empty arrays.
🎯 Interview Key Takeaway
Only 8 values are falsy in JS. Empty arrays [] and empty objects {} are truthy!
#Truthy#Falsy#Booleans#Beginner
Back to all JavaScript & TypeScript Questions→