What is Truthy and Falsy in Javascript?

Hi, in this post I’m going to explain you what is Truthy and falsy in Javascript and you will see some examples.

Truthy

Truthy is a value that is considered true when encountered in a Boolean context.

Examples:

true
'false' // All string that have something is Truthy
{}
[]
'string'
3.1416
new Date()

Falsy

Falsy is a value that is considered false when encountered in a boolean context.

Examples:

false
0 // zero
"" // All string that is empty is falsy
null
undefined 
NaN // Not a Number

Ok cool, now if we apply some negations and comparisons, here some examples:

!!(0); // false -> !(0) = true and !true = false

!!("0") // true -> !("0") = false and !false = true

false == 0 // true -> true means that this condition is true, so 0 is equal to false.

false == "" // true 

0 == "" // true

Nice, now the undefined and falsy null are not equals to nothing, except itself:

null == false // false

null == null // true

undefined == undefined // true

undefined == null // true

The falsy NaN is not equal to nothing, including NaN:

NaN == null // false
NaN == NaN // false

Strict validations

false == 0 // true
false === 0 // false

By Cristina Rojas.