Truthy and Falsy Values in Javascript

In Javascript, things can be true, or false, but they can also be truthy or falsy. The concept of truthy and falsy are usually considered only in a boolean context, such as in an if..else statement, but they also affect other parts of Javascript – for …


This content originally appeared on DEV Community and was authored by Johnny Simpson

In Javascript, things can be true, or false, but they can also be truthy or falsy. The concept of truthy and falsy are usually considered only in a boolean context, such as in an if..else statement, but they also affect other parts of Javascript - for example, what is returned from the logical OR operator.

What does truthy and falsy mean in Javascript?

Something is considered falsy in Javascript if it can be converted to false. In the same way, something is truthy simply if it is not considered falsy. While falsy may seem like a vague statement, it actually has a specific definition. The following values are considered falsy:

  • false
  • 0 or -0 or 0n
  • any empty string, i.e. ""
  • null
  • undefined
  • NaN

Similarly, anything that is not equal to these is considered truthy, for example:

  • any object, i.e. {}, or [].
  • any non-zero number
  • any non-empty string
  • any non null, undefined, or NaN value.

Use of truthy and falsy

truthy and falsy values have implications in Javascript logic. For example, anything that can be converted to truthy in a boolean setting (such as if..else) is converted to true, and the same goes for falsy statements.

For example, the following if statement is true, simply because "1" is a truthy statement:

if("1") {
    console.log('ok');
}

If the statement instead said if(0), it would return false, since 0 is falsy. As you get into Javascript, you'll find references to truthy and falsy everywhere, so it's good to familiarise yourself with the concept now.


This content originally appeared on DEV Community and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-07-12T18:00:21+00:00) Truthy and Falsy Values in Javascript. Retrieved from https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/

MLA
" » Truthy and Falsy Values in Javascript." Johnny Simpson | Sciencx - Tuesday July 12, 2022, https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/
HARVARD
Johnny Simpson | Sciencx Tuesday July 12, 2022 » Truthy and Falsy Values in Javascript., viewed ,<https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/>
VANCOUVER
Johnny Simpson | Sciencx - » Truthy and Falsy Values in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/
CHICAGO
" » Truthy and Falsy Values in Javascript." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/
IEEE
" » Truthy and Falsy Values in Javascript." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/. [Accessed: ]
rf:citation
» Truthy and Falsy Values in Javascript | Johnny Simpson | Sciencx | https://www.scien.cx/2022/07/12/truthy-and-falsy-values-in-javascript-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.