المعرفة:: JavaScript الحالة::مؤرشفة المراجع:: https://developer.mozilla.org/en-US/docs/Glossary/Falsy, https://developer.mozilla.org/en-US/docs/Glossary/Truthy, The Complete JavaScript Course 2022 From Zero to Expert


Falsy

falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.

ValueDescription
falseThe keyword false.
0The Number zero (so, also 0.0, etc., and 0x0).
-0The Number negative zero (so, also -0.0, etc., and -0x0).
0nThe BigInt zero (so, also 0x0n). Note that there is no BigInt negative zero — the negation of 0n is 0n.
"", ”, “Empty string value.
nullnull — the absence of any value.
undefinedundefined — the primitive value.
NaNNaN — not a number.
document.allObjects are falsy if and only if they have the IsHTMLDDA internal slot.That slot only exists in document.all and cannot be set using JavaScript.

In short:

  • 0
  • "" (Empty String)
  • undefined
  • null
  • NaN

Examples of falsy values

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")

Truthy

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. That is, all values are truthy except false0-00n""nullundefined, and NaN.

Note: Empty Objects {} and Arrays [] aren’t falsy!

Examples of truthy values

if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)