What is truthy and falsy in JavaScript
· Category: JavaScript
Short answer
In JavaScript, every value is either truthy or falsy. Falsy values coerce to false in boolean contexts, while truthy values coerce to true.
How it works
The six falsy values are:
false0and-0and0n''(empty string)nullundefinedNaN
Everything else is truthy, including:
'0'and'false'(non-empty strings)[]and{}(empty collections)Infinity
Example
if ('0') { console.log('truthy'); } // logs truthy
if ([]) { console.log('truthy'); } // logs truthy
if (!!'') { console.log('never'); } // does not log
Why it matters
Relying on truthiness makes code concise but risky. Explicit checks like value != null or Array.isArray(arr) && arr.length > 0 avoid subtle bugs with empty objects and strings.