In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy.
Examples of Truthy values are: (which will be coerced to true in boolean contexts, and thus execute the if
block):
if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-92)
if (19n)
if (660)
if (-2.50)
if (Infinity)
if (-Infinity)
List of falsy values in JavaScript are:
false
null
undefined
0
NaN
''
,""
,``
(Empty template string)
Example of Falsy value are:
if (false)
if (null)
if (undfined)
if (0)
if (Nan)
if ("")
if ('')
if (``)
There is another way of converting Truthy and Falsy values to Boolean are:
!!("0") // returns true!! ("2") // returns true