paint-brush
What are Truthy and Falsy Values in JavaScript?by@smpnjn
499 reads
499 reads

What are Truthy and Falsy Values in JavaScript?

by Johnny Simpson
Johnny Simpson HackerNoon profile picture

Johnny Simpson

@smpnjn

Product, Engineering, Web

July 12th, 2022
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.
featured image - What are Truthy and Falsy Values in JavaScript?
1x
Read by Dr. One voice-avatar

Listen to this story

Johnny Simpson HackerNoon profile picture
Johnny Simpson

Johnny Simpson

@smpnjn

Product, Engineering, Web

About @smpnjn
LEARN MORE ABOUT @SMPNJN'S
EXPERTISE AND PLACE ON THE INTERNET.

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, like 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.




L O A D I N G
. . . comments & more!

About Author

Johnny Simpson HackerNoon profile picture
Johnny Simpson@smpnjn
Product, Engineering, Web

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Also published here
Newsbreak
Platypush
Coffee-web
Trendcdn
Jakobs
Asorrybowl
Dedyn
Newsbreak
Hashnode
X REMOVE AD