Truthy and Falsy in Javascript

·

1 min read

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

Github-https://github.com/DIWAKARKASHYAP