Check out a free preview of the full Deep JavaScript Foundations, v3 course:
The "toBoolean" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle discusses the behavior of the abstract operation toBoolean.

Get Unlimited Access Now

Transcript from the "toBoolean" Lesson

[00:00:00]
>> Kyle Simpson: And finally, let's look at the ToBoolean abstract operation. And by the way, these four are what we're looking at, two primitive, two string, two number, and two Boolean. There are other abstract operations that they refer to and use. So read those in the spec if you're curious, but these are the major ones you're gonna run into.

[00:00:14] So ToBoolean, section 7.1.2 in the spec. Anytime you have any value that is not a Boolean, and it's used in a place that needs a Boolean, this operation occurs. Exactly the same as the other ones, okay? This operation is less algorithmic and more lookup. There's not really anything to do, other than to say, is the value, what we call falsy, or not, that's really the only question here.

[00:00:43] So there's essentially a look up table. Literally in the spec, that says, here is the value of one of these things if so return false, meaning the false value is what came back from ToBoolean. And otherwise just return true. So it defines a very specific and short limited list of what we call falsy values.

[00:01:03] That is values that will return false when coerced to a Boolean, and any of the various ways that you can coerce to a Boolean. These are the falsy values. The empty string, either of the 0, the null, the NaN, the false, and the undefined. Those are our falsy values.

[00:01:25]
>> Kyle Simpson: If it's not on that list, it is always truthy, okay? If it's not on the list, it is truthy. So some examples of things on that list, that list is infinitely long. But some examples would be a string, a number, an object, an array, true, function. Those are all truthy values.

[00:01:44] Let's see if you're paying attention. What would happen if you try to two Boolean an empty array? Would it be truthy or would it be falsy? So pay very close attention to how I describe to ToBoolean, it does not invoke the two primitive algorithm. Or the two number, or the to string, or anything, it just does a look up.

[00:02:09] So is the empty array on the falsy the list?
>> class: No.
>> Kyle Simpson: So what must it return?
>> class: Truthy.
>> Kyle Simpson: True, okay? That's a key thing to keep in your mind when you're doing to Booleans, when that's what's happening, there's no other coercion stuff happening. It's just a straight look up, is it there or is it not.

[00:02:27] You can't override it with a value over to string or anything. It's just is it on the list or is it not, okay? So that's how we get our falsy and truthy values. Essentially, memorize the falsy the list, and then always ask is the value on that list if so falsy, otherwise, it must be truthy.