Programming Language

Meaning Of Falsy In Javascript

We already know that Javascript has the Boolean values, both false and true. However, what is important and interesting is that you must understand the fact that everything  has a built-in Boolean value in Javascript in addition to its ‘apparent’ value, and that Boolean value is known as either truthy or falsy. Although, this may sound puzzling, however, read on and the concept will become well-defined.

List of Javascript’s Falsy Values

The complete list of falsy values in Javascript is as follows:

  • false
  • 0 (zero)
  • “” (an empty string)
  • null
  • unclear
  • NaN- a possession that demonstrates the Not-a-Number (NaN) value showing that a value is not a legal number)

Javascript’s Truthy values

Now you may be wondering what are truthy values of Javascript. Well, each value that is not available in the above list is a truthy value.

Even if we select false and place it in quotes – like this: “false” – then that it would still be a truthy value.

Tests for Javascript Falsy:

It is important to test different falsy values to see how they match to each other.

False, “”, and 0 – all falsy values – can be compared to one another and are considered to be equivalent. This is how it would look like:

“” == false //this is true

0 == false //this is true

0 == “” //this is even true

However, the values null and undefined are considered to not be equal to anything but themselves. Thus, here is what we would have:

(null == null) // true

(undefined == null)// true

(null == false) // false

 (undefined == undefined) // true

The odd thing about NaN is that it is not regarded as equal to anything and that involves itself:

  • NaN == NaN //false
  • NaN == undefined //false

It is necessary to be very careful while writing the ‘if’ statements, specifically when you are analyzing properties that have the potential to come back as a falsy value. In such situations, you should be prepared to deal with it in a proper manner.

Leave a Reply