only 7 "falsy" values in JS.
JS โฉ value โฉ type โฉ conversion โฉ falsy
7 falsy values๏ผ(converts to false)
false, 0, 0n, null, undefined, NaN, "" (empty string).
false
0
0n
null
undefined
NaN
""
any other value is "truthy", and converts to true.
every value is either truthy or falsyโ
all objects are truthy (convert to true).
operator(s) that expect boolean operands will work with an operand of any typeโ
Boolean
arr.filter() - remove falsy values.
'0' == false // trueโ๏ธ if ('0') console.log('hi') // hi (โญ๏ธ '0' is truthyโ๏ธ) '0' === false // falseโ๏ธ // facts about empty array [] == ![] // trueโ๏ธ [] == [] // falseโ๏ธ Boolean([]) // true Boolean(![]) // falseโ๏ธ
sitepoint โฉ Truthy and Falsy: When All is Not Equal in JavaScript
In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?
Last updated 2 years ago
Was this helpful?