Truthy and Falsy in Javascript
In JavaScript, a truthy value is a value that is considered true
when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).
Truthy values include:
"non-empty string"
{}
[]
'false' (as a non-empty string)
42 (or any non-zero number)
new Date()
-42 (or any non-zero negative number)
3.14 (or any non-zero number)
Infinity
-Infinity
true
Promise.resolve()
Symbol()
() => {}
Falsy values include:
false
0
-0
0n (BigInt zero)
"" (empty string)
null
undefined
NaN
Any variable that contains these values would be considered falsy when converted to a Boolean. You can test this by using the Boolean()
function or a double NOT operator !!
in JavaScript. For instance, Boolean(null)
or !!null
would return false
Thank you for reading, please follow me on Twitter, i regularly share content about Javascript, and React and contribute to Opensource Projects
Twitter-https://twitter.com/Diwakar_766
Subscribe to my newsletter
Read articles from Diwakar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Diwakar
Diwakar
As a passionate developer, I thrive on acquiring new knowledge. My journey began with web development, and I am now actively engaged in open source contributions, aiding individuals and businesses.