If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
A fundamental concept in JavaScript is truthy and falsey values. And it sounds hard, but beyond the little bullion, false, there are only a few falsey values and they are quite intuitive. Basically, any value that behaves like a false and a conditional is called falsey and everything else is called truthy. Now, instead of thinking about which values are truthy, you can basically just memorize the few values that are falsey. Of course, the little Boolean false by itself is falsey. So this particular console log statement is never going to execute. And additionally, the special values and null and undefined are both falsey.
00:34
In terms of strings, there is only one string, which is falsey, which is the empty string. And within the number data type, the values zero and nan are both falsey. The final value that is falsey within modern Dao escaped is the big end zero, which we can represent with zero N. And this is it. This is the entire set of falsey values. And because they are all falsey, none of the console.blog statements will execute. And we can verify that by running this code. Theological complement operator also called the bank, represented by the exclamation point, can be used to convert to the and falsey values, to the bullion two and false equivalents applying the not operator to bullion
01:11
false turns into true. And the same thing happens for all of the other falsey values as well. So not null, not undefined, not empty string, not zero, not nan, not big in zero, they all turn to true. We can actually use this operator to transparently convert Falsey values to false. And two, three values to true. If you apply it only once, then false turns to true and two turns to false. But if we apply it twice, then false remains false and true remains true. And what is more interesting is that a single not operator converts all of the values to their bullion complement, applying it twice,
01:46
converts it to their bullion equivalent. So for all of the Falsey values that exist within JavaScript, we get back the literal false. And for everything else we get back the literal true. This usage of the not operated device has a number of use cases within JavaScript. Consider the requirement that given an array, we want to determine if it has any items. Now, when the array doesn't have any items, it'll have a length of zero and we can easily convert that into the Boolean. Using the Notnot operator has items is going to be false in this particular case. And for all other cases where the array does have items,
02:20
length is not going to be zero and therefore Notnot is going to give us the value. True. People often confuse truthy and falsey with loose equality type coercion. They are not the same. And as we discussed in our lesson on equality, loose equality is just bad. Toothy and falsey on the other hand, is something that you can depend upon. Let's create a simple function that logs out the toothy or falsey nature of a value. If the value is going to be toothy, the conditional is going to resolve to toothy, otherwise it is going to resolve to falsey. Now we already know the few values within JavaScript that are falsey, which include the number zero and the empty string. Now,
02:56
every other string other than the empty string Is going to be toothy and JavaScript. And this is something that you can depend upon. You know that J is going to be toothy, the character is zero, is going to be toothy just like every other non empty string compared to this, loose equality is pretty unreliable. As an example, it makes sense that zero is equal to false and empty string is equal to false, but a string containing the character zero is also equal to false. And you might think, Hey, sure, I can just exclude this one special case. But you can actually generate a string as long as you want, as long as it contains zeros, it'll be equal to false. So in summary,
03:30
the concept of truth and falsey is not the same as lose equality with false. They are completely different. I hope you enjoyed this tutorial. As always, thank you for joining me and I will see you in the next one.