TOC

The community is working on translating this tutorial into Italian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

The Basics:

Truthy & Falsy

In most programming languages, as well as in math, the concept of true and false is completely binary: Either something is true, otherwise it's false. But in JavaScript, the concept of true and false is expanded a bit, because the language is generally quite loose and not as strict as other programming languages.

In practice this means that sometimes JavaScript will evaluate something to either true or false, despite the fact that it's not completely true or false. The purpose is simply to make things easier and less complicated for you, but if you don't know about this, or if you forget it for a moment, things might get a bit confusing.

So, allow me to introduce a new concept for you: Truthy and Falsy. They exist only as concepts - you won't find keywords for them in JavaScript, nor will you be able to check against a truthy or falsy value. Instead, JavaScript has introduced a second set of operators for checking whether something is completely true or completely false. These are referred to as strict operators, e.g. the strict equality operator, where we use three equal signs instead of two, when comparing two values.

We will talk much more about the various operators of JavaScript, including the strict ones, in an upcoming chapter, but for now, let's talk more about what can be considered Truthy or Falsy.

Truthy

In JavaScript, all values are Truthy unless they are specifically Falsy. JavaScript will use type coercion to convert something to a boolean value when used in a context which requires it, like a loop or an if statement. For instance, consider the following examples of values that will be treated as true because they are Truthy:

if(true)
	alert("truthy");
if(42)
	alert("truthy");
if("hello world")
	alert("truthy");	

If you run this example, you will see that all three if statements evaluate to true, but why is "hello world" true? Because it contains an actual value and therefore, JavaScript will consider this truthy when used in a context where true or false is required.

Falsy

In general, all values that are either null, undefined or empty will be considered as Falsy and therefore be treated as false, when used in a boolean context like loops and if statements. Here's a couple of examples:

if(false)
	alert("falsy");
if(null)
	alert("falsy");
if(0)
	alert("falsy");	
if("")
	alert("falsy");	

If you run this sample, you will notice that none of the alerts are triggered - all of the above if statements will be considered as false in this context.

Summary

When JavaScript needs something to be either true or false, for instance when evaluating the condition for a loop or an if-statement, it will resort to type coercion, if necessary, to turn values that are not actual booleans into true or false. The result of this is referred to as Truthy and Falsy, because while they are not actually true or false, they are close enough for JavaScript to be able to make a decision based on them, as illustrated by the examples of this article.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!