Truthy and Falsy Evaluations
Six rules decide every truthy question. Free to watch, with a free slide PDF and practice questions.
Chapters
- 0:00 Truthy and Falsy Evaluations
- 0:36 Quick recap: four rules for mixed types
- 1:16 Six rules decide every truthy question
- 1:57 One table holds the whole lesson
- 3:16 Falsy is a short, closed list
- 4:14 Everything not on the list is truthy
- 5:20 An empty array still runs the block
- 5:55 Boolean() and !! give the explicit answer
- 6:50 || returns the first truthy operand
- 7:50 && returns the first falsy operand
- 8:44 && binds tighter than ||
- 9:25 ?? falls back only for null and undefined
- 10:22 A zero discount is not a missing discount
- 11:04 == is not truthiness
- 11:45 Truthy in an if, yet equal to false
- 12:48 Six terms to say in your own words
- 13:26 Rewrite with ??, then compare the inputs
- 14:49 Eight questions, closed book
- 15:45 A developer runs the following code. What is logged?
- 16:22 10 0
- 17:17 Four rules for truthy and falsy
- 18:07 Now test it, then move to arrays
Test yourself on truthy and falsy evaluations
Practice questions for this lesson come with a full explanation of why each answer is right or wrong, and link back to the exact moment in the video.
Take the free diagnosticTranscript
Read the full lesson text
Truthy and Falsy Evaluations 0:00
Welcome to module one, lesson four: truthy and falsy evaluations. This module is called Variables, Types, and Collections, and it carries twenty three percent of the exam. In this lesson we ask which values count as true and which as false, and what the logical operators really hand back. The or operator, the and operator, and the nullish operator each have their own rule. By the end, you will be able to name every falsy value, predict what each of those operators returns, and spot the lookalike trap before you run it.
Quick recap: four rules for mixed types 0:36
Before we start, a quick recap from lesson three. First, plus concatenates when either side is a string, and it works left to right. Second, minus, times, and divide convert to numbers. Null becomes zero, and undefined becomes nan. Third, double equals converts types, and triple equals never does. And fourth, null equals only undefined, even under double equals. Today we ask a different question. Not what a value converts to, but how it behaves in an if. Which values count as true or false, and what do the logical operators return?
Six rules decide every truthy question 1:16
This domain is twenty three percent of the exam. This lesson boils down to six rules. Two of them are about the defaulting operators, the or operator and the nullish operator. First, the falsy list. Second, the truthy lookalikes. Third, explicit boolean conversion. Fourth, the or and and operators return operands, not booleans. Fifth, nullish versus or. And sixth, double equals is not truthiness. Rules two and five produce most of the traps, so we slow down there. Memorize this: the falsy list is short and closed, and everything else is truthy.
One table holds the whole lesson 1:57
This is the foundations slide, and one table holds the whole lesson. Every remaining slide is one row of this table, with an example, so pause here and read each row aloud. Falsy list. The rule is that it is a short, closed list. The trap is forgetting negative zero, big int zero, or nan. The safe habit is to memorize it. Lookalikes. The rule is that everything else is truthy. The trap is that the string zero, an empty array, and an empty object are all truthy. The safe habit is to test against the list. The logical operators, or and and. The rule is that they return an operand. The trap is that the result is not always true or false. The safe habit is to trace left to right. Nullish. The rule is that it falls back only on null or undefined. The trap is that the or operator overwrites zero and the empty string. The safe habit is to default with the nullish operator. Double equals. The rule is that it has its own coercion steps. The trap is that null double equals zero is false. The safe habit is to use triple equals. One practical rule to carry with you: know the list, then trace operands left to right.
Falsy is a short, closed list 3:16
Rule one: the falsy list. Falsy is a short, closed list. Look at the code. Line one is false. Line two is zero, negative zero, and zero n, which is big int zero. Line three is the empty string, written with double quotes, single quotes, or backticks. Line four is null, undefined, and nan. And line five is "document dot all". Count them. That is every falsy value, and nothing else is on the list. A falsy value is treated as false in an if or a loop test. Null and undefined are also called nullish. Zero n is big int zero, and an empty template literal is also falsy. And "document dot all" is the only falsy object. Every other object is truthy. The exam point: memorize the falsy list. If a value is not on it, it is truthy.
Everything not on the list is truthy 4:14
Rule two: everything that is not on the list is truthy. Look at the code. Line one, Boolean of the string zero, is true. Line two, Boolean of the string false, is true. Line three, a string holding a single space, is true. Line four, an empty array, is true. Line five, an empty object, is true. Line six, a new Date, is true. Line seven, negative forty two, is true. Line eight, twelve n, a big int, is true. And line nine, Infinity, is true. A string with any characters in it is always true, including the string zero and a single space. Empty arrays and empty objects are truthy. Negative numbers, big ints, Infinity, and a Date are all truthy too. Every value is truthy unless it is on the falsy list. The exam point: an empty array, an empty object, the string zero, the string false, and a single space are all truthy.
An empty array still runs the block 5:20
Now the trap behind that rule. The wrong shortcut says, it is empty, so it is falsy. The correct exam test asks, is the value on the falsy list? Look at the code. If an empty array, the block runs. If an empty object, the block runs. If the string zero, it runs. If the string false, it runs. But if zero, the block is skipped. Empty does not mean falsy for arrays and objects. The exam point: if an empty array runs its block, because an empty array is not on the falsy list.
Boolean() and !! give the explicit answer 5:55
Rule three: explicit boolean conversion. Boolean, and the exclamation mark, which people call bang, give you the explicit answer. Look at the code. Console dot log of double bang, a non empty string, prints true. Double bang null is false. A single bang on zero is true, because bang inverts. Boolean of zero is false. Boolean of the string x is true. And double bang x, triple equals, Boolean of x, is true, because the two forms are the same. Boolean of a value returns the explicit boolean for any value. A single bang converts to boolean and inverts. Double bang value equals Boolean of value, because it inverts twice. Logical operations convert to boolean the same way. The practical rule: when you need a real true or false, convert on purpose.
|| returns the first truthy operand 6:50
Rule four: the or and and operators return operands, not booleans. Let's start with or, written as a double pipe. Look at the code. Line one: null or zero or one. Null is falsy and zero is falsy, so the result is one. Line two: undefined or null or zero. Nothing is truthy, so the last operand comes back, which is zero. Line three: the empty string or the string guest. The empty string is falsy, so we get the string guest. When no operand is truthy, the last operand comes back, as line two showed. The or operator evaluates left to right. It returns the first truthy operand. If none is truthy, it returns the last operand. And it short circuits: the rest is never evaluated. The exam point: or returns one of its operands, not necessarily true or false.
&& returns the first falsy operand 7:50
Now its mirror image: the double ampersand, the logical and operator. Look at the code. Line one: one and two and null and three. It stops at null, which is falsy, and returns null. Line two: one and two and three. Everything is truthy, so three comes back. Line three: an empty array and the string dog. We get the string dog. Line four: zero and the string dog. Zero is falsy, so it returns zero. And returns the first falsy operand. If all are truthy, it returns the last operand. So the empty array and the string dog gives the string dog, because an empty array is truthy. It also short circuits at the first falsy operand. The exam point: or yields the first truthy, else the last. And yields the first falsy, else the last.
&& binds tighter than || 8:44
One more wrinkle: precedence. The and operator binds tighter than the or operator, so and runs first. Look at the code. Null or two and three or four. Because and binds tighter, we group two and three first. That gives null or three or four. Null is falsy, and the first truthy operand is three, so the result is three. The order is: the exclamation mark first, then the and operator, then the or operator. When in doubt, add parentheses, then trace left to right. The exam point: null or two and three or four gives three.
?? falls back only for null and undefined 9:25
Rule five: the nullish operator, written as a double question mark, versus or. Look at the code. Height is zero. Height or one hundred gives one hundred, because zero is falsy. Height, double question mark, one hundred gives zero, because zero is not null or undefined. Now name is the empty string. Name or the string guest gives the string guest. Name, double question mark, guest gives the empty string, which is kept. The or operator returns the first truthy value. The nullish operator returns the first defined value. So or can overwrite a meaningful zero or empty string. Nullish keeps zero, the empty string, false, and nan. Nullish means null or undefined, and only those two. The exam point: for a default that must preserve zero or the empty string, use the nullish operator, not or.
A zero discount is not a missing discount 10:22
A trap that hides in real code. The wrong shortcut says, or is the normal way to set a default. The correct exam test asks, is zero or the empty string a valid value? Then use the nullish operator. Look at the code. The discount is zero. Rate one is discount or five, which gives five, so the zero is lost. Rate two is discount, double question mark, five, which gives zero, so the zero is kept. This is the sneaky bug: it only shows up when the real value is zero. The practical rule: default with the nullish operator when zero, the empty string, and false are legitimate inputs.
== is not truthiness 11:04
Rule six: double equals is not truthiness. The wrong shortcut says, null and zero are both falsy, so null double equals zero. The correct exam test says, double equals uses its own steps, and null equals only undefined. Look at the code. Null double equals zero is false. Zero double equals null is false. Null double equals undefined is true. And zero double equals false is true. Falsy is about conditions. Loose equality is its own algorithm. The exam point: double equals follows its own coercion rules, not the falsy list.
Truthy in an if, yet equal to false 11:45
This is the classic puzzle. An empty array is truthy in an if, yet it is double equals false. Look at the code. In an if, the empty array is truthy, so the block runs. Yet an empty array double equals false is true. Why? Here are the steps. False becomes zero, so we compare the array with zero. The array becomes the empty string, so we compare the empty string with zero. The empty string becomes zero, and zero equals zero. Three conversions, in order. A boolean becomes a number, so false is zero. An object becomes a primitive, so an empty array becomes the empty string. And a string meeting a number becomes a number, so the empty string becomes zero. So an empty array double equals false is true, yet the array is still truthy. These are two different questions with two different algorithms. The exam point: an empty array is truthy in an if, but an empty array double equals false is true.
Six terms to say in your own words 12:48
Six terms to say in your own words. Falsy means treated as false in a Boolean context. Truthy is any value that is not falsy. Nullish means null or undefined. Short circuit means the or and and operators stop once the result is decided. Nullish coalescing is the double question mark, which uses the right side only for null or undefined. And double negation is double bang x, the same as Boolean of x. Check yourself. Pause the video now, cover each card, and explain the term aloud.
Rewrite with ??, then compare the inputs 13:26
Now the proof stage, starting with a lab. Rewrite the or line with the nullish operator, then compare the inputs. Look at the code. Before uses the or operator, and after uses the nullish operator, with the same fallback, the string n slash a. You have six inputs on screen. For each one, predict what each version returns. Write both columns down on paper before checking, then run it in Node. Take two minutes on your own. Now the answers, one input at a time. Input one, zero. Or gives the string n slash a, and nullish gives zero. It changes. Input two, the empty string. Or gives n slash a, and nullish keeps the empty string. It changes. Input three, false. Or gives n slash a, and nullish keeps false. It changes. Input four, nan. Or gives n slash a, and nullish keeps nan. It changes. Input five, null. Both give n slash a. They are the same. Input six, undefined. Same again: both give n slash a. Only the four falsy inputs that are not nullish change behavior. A wrong prediction is the lesson. Go back to the slide that explains it.
Eight questions, closed book 14:49
Self check: eight questions, closed book. Answer out loud, then I will reveal each one. One. Boolean of the string zero? True, because it is a string with a character in it. Two. Boolean of an empty array? True. Three. Double bang nan? False, because nan is on the list. Four. Null or zero or one? One. Five. One and two and null and three? Null, the first falsy operand. Six. Zero, double question mark, ten, and then zero or ten? Zero, and then ten. Seven. Null or two and three or four? Three, because and runs first. And eight. Is null double equals zero? False. How many did you get? Anything you missed, go back to its slide.
A developer runs the following code. What is logged? 15:45
Time for a practice question, taken from the question bank. This question contrasts the two defaulting operators from this lesson. A developer runs the code shown. What is logged? Let qty equal zero. Console dot log of qty or ten, comma, qty double question mark ten. Option A: zero, zero. Option B: ten, zero. Option C: ten, ten. Option D: zero, ten. Pause the video and decide what each operator does with zero. I will give you the answer on the next slide.
10 0 16:22
The answer is B. Ten, zero. Here is why. Qty is zero, and zero is falsy. So qty or ten replaces the zero and gives ten. But qty, double question mark, ten keeps the zero, because zero is not null or undefined. That gives ten, then zero. Now the wrong options. A says zero, zero. That thinks the or operator keeps zero, but or replaces any falsy value. C says ten, ten. That thinks the nullish operator also replaces zero, but it only replaces null and undefined. D says zero, ten. That swaps the two operators, since the or operator is the one that replaces zero. The takeaway: or replaces any falsy value, and the nullish operator replaces only null and undefined.
Four rules for truthy and falsy 17:17
Let's consolidate. Four rules for truthy and falsy. The list: falsy is closed. False, zero, negative zero, zero n, the empty string, null, undefined, nan, and "document dot all". Lookalikes: the string zero, the string false, a single space, an empty array, and an empty object are all truthy. Operands: the or operator returns the first truthy, and the and operator the first falsy, otherwise the last. Defaults: the nullish operator replaces only null and undefined, and double equals is not truthiness. Remember: if a value is not on the list, it is truthy. If you can say these four cards from memory, you've got the lesson.
Now test it, then move to arrays 18:07
That completes lesson four. Now test it, then move on to arrays. The flow is: read it, predict it, test it. Your practice set is this lesson's questions in the bank. Do them now, closed book, and map any miss back to the slide that explains it. In the next lesson, Manipulating Data with Arrays, we work with lists of data: which array methods change the original, and which return something new. See you there.