Truthiness
A value's truthiness determines if it is treated as true
(truthy) or false
(falsy) when used in a boolean context.
Control Flow Examples
if ('0') console.log('it runs');
'it runs'
if (0) console.log('truthy');
else console.log('falsy');
'falsy'
let i = 0;
while (i) i++;
console.log(i);
0
Converting to Boolean
Truthy Examples
Boolean(true)
true
// `new` makes an object
// this is not a primitive value
Boolean(new Boolean(false))
true
Boolean(' ')
true
Boolean([])
true
Boolean(Infinity)
true
Falsy Examples
Boolean(false)
false
Boolean(0)
false
Boolean(NaN)
false