Truthy vs Falsy in JavaScript

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

Truthy vs Falsy in JavaScript

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Falsy Values

if (false) { console.log('Will only log if truthy') }

if (null) { console.log('Will only log if truthy') }
if (undefined) { console.log('Will only log if truthy') }

if ('') { console.log('Will only log if truthy') }

if (0) { console.log('Will only log if truthy') }
if (NaN) { console.log('Will only log if truthy') }

if (0n) { console.log('Will only log if truthy') }

Boolean Coercion

let array, hasItems;

array = [];

hasItems = !!array.length;
console.log(hasItems); // false

array = ['hi', 'there'];

hasItems = !!array.length;
console.log(hasItems); // true

Ternary Conditional

function logNature(value) {
console.log(value ? 'Truthy' : 'Falsy');
}

logNature(0); // Falsy
logNature(''); // Falsy
logNature('j'); // Truthy
logNature('0'); // Truthy
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

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.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36