JavaScript Tutorial

Truthy & Falsy Values

Truthy and falsy values are related to boolean values true or false, especially used in the context of condition checking. For example, in an if statement for checking between different values—

Example: If data exists, then the if condition will check like this:

if (data) {
  console.log('data exists');
}

Here, data is considered as a truthy value, aka true.

Meaning: In this context, the value of data is not one of the falsy values, so JavaScript treats it like true and executes the block.


Falsy (aka false) values in JavaScript are:

  • number zero 0
  • negative zero -0
  • null
  • undefined
  • NaN (not a number)
  • empty string ""
  • false (of course)

These are treated as false when used in conditions.


Truthy (aka true) values include:

  • true
  • empty array []
  • empty object {}
  • non-empty string like "0"
  • string "false"
  • any non-zero number like 5, -10
  • Infinity, -Infinity
  • string with space " "
  • function function(){}
  • any object with content {name: "nimmi"}
  • any array with content like ["hello"]

So all these truthy values will make the if condition run.

Let me know if you want this in a table form or with more real use cases like login checks or API responses!

if ('') console.log('Falsy'); // Empty string is falsy
if (0) console.log('Falsy'); // Zero is falsy

Questions & Answers Related

No Q&A available for this topic.