Truthy and Falsy in javascript

For the programmers who are new to javascript, the terms Truthy and Falsy can be confusing. So lets understand Truthy and Falsy.

Anything which evaluates to TRUE in a logical execution or in a boolean context can be said Truthy. All others are Falsy in javascript.

The best way to learn is write some codes and execute that; so lets write a function which accepts a value and checks whether the value is Truthy or Falsy and print accordingly.

Note:- you can click f12 in your browser and click on console and there you can run the above codes

We can pass a number of values to the above function and check ‘truthiness’. Before that we can take a look into what are Falsy in javascript.

Falsy in javascript

Generally there are some values which will be always Falsy. They are..

  • false
  • 0(number zero)
  • “”(empty string)
  • null
  • undefined
  • NaN(Not a number)

Now just check them using the function we wrote above.

Note:- you can click f12 in your browser and click on console and there you can run the above codes

Ok, now we know all of the above are Falsy. There are other cases which may get confused easily.

consider ‘false’. What will be the out put if we pass this to the function we created.

lets check that

Oops… its Truthy. How is that be Truthy? The answer is ‘false’ is not a boolean value. It is a string here (see the single quotes around false).

The same scenario applies for ‘0’ (zero with quotes).

Note:- Using double quotes(“”) instead of single quotes for ‘false’ and ‘0’ will have the same results as javascript treats them as same.

just check them also.

Truthy in javascript

Unlike Falsy there are lot of Truthy situations. Well we can say everything that are not Falsy will be Truthy.

Some of the Truthy values which may be confusing are given below.

  • true(boolean value)
  • {}
  • []
  • ‘some string’
  • 10(Any number)

So an object of any kind(including functions, arrays, objects) are always Truthy. So {} and [] are Truthy even though they are empty.

Wrapping primitive

There are some cases which will make Falsy values Truthy. For example we saw that the 0(zero) is Falsy; but new Number(0) is Truthy. Also new Boolean(false) is Truthy. We can check this with the above function created.

So the primitive Falsy values can be made Truthy by wrapping with Objects.

Comparing Falsy values

Even though the values listed at the top are Falsy, that doesn’t mean that they are equal to each other. But of course, some of them are equal in . We can have a look into it.

There are five Falsy values which are equal to each other and they are divided into two sets. They are

Set A

1. 0
2. false
3. “” (Empty String)

We can ensure these by executing the following code.

Set B

Other set which are equal to each other is

1. Null
2. undefined

To check execute the following code

But please note that these two ‘sets’ are not equal to each other; means one value from first set is not equal to any value in the second set. See the examples below.

Note:- you can click f12 in your browser and click on console and there you can run the above codes

The NaN

There is a special guy who will not be equal to others, not even to himself!!!. Interesting isn’t it?. He is NaN; the ‘Not a Number’.
NaN will not be equal to any Falsy value from from SET A or SET B. To verify execute the codes below.

And one thing to not about NaN is that two NaN will not be equal.
See the code below.

Truthy and Falsy in Runtime

Ok; now we know what are Truthy and Falsy. Let us take a look how they are used. For that first we will create a scenario and then we will write the code to meet the scenario. The scenario follows.
I need to write a function to print/log the person name.
Conditions
1. If person has both last name and first name then print both first name and last name separated by a space in between them.
2. If person has only first name then print first name and if person has only last name just print last name.

Simple!!! isn’t it? All you need to do is just check whether the person has last name or first name or both. If you know programming languages like java you would probably check like

That is we need to check the lastName against “”(empty String) using an “==” operator or “.equals()” method to check whether it is available or not.

But in javascript you don’t need to check against an empty string. You can just check its “truthy-ness”; as follows.

So we can write the code for the scenario described above.

Thats it. This is how we use Truthy and Falsy in javascript.

In some cases you need to check exact type of a value. For instance for the above scenario if we want check wether the person.lastName is null or not we can use === or !== as follows.

The above code prints only if lastName is null. And it will not print if person.lastName=undefined or “”(empty String) or any other value.

Leave a Comment

Your email address will not be published. Required fields are marked *