JavaScript has truthy and falsy values. Truthy values are Boolean values that resolve to true
, while falsy values are values that resolve to Boolean false
.
All other values are truthy values in JS unless they are defined as falsy.
The following are also truthy values:
‘0’
(a string containing a single zero)"0" && true
>> true
"0" && false
>> false
"0" || true
>> "0"
‘false’
(a string containing the text “false”)"false" && true
>> true
"false" && false
>> false
"false" || true
>> "false"
[]
(an empty array)[] && true
>> true
[] && false
>> false
[] || true
>> []
{}
(an empty object){} && true
>> true
{} && false
>> false
{} || true
>> {}
function(){}
(an “empty” function)(function name(){}) && true
>> true
(function name(){}) && false
>> false
(function name(){}) || true
>> [Function: n]