JavaScript: From First Steps to Professional

Boolean Q&A

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
JavaScript: From First Steps to Professional

Check out a free preview of the full JavaScript: From First Steps to Professional course

The "Boolean Q&A" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana answers a student's question regarding how fact.answer works since it compares a string to a boolean and demonstrates how to convert a non-string value into a string using toString.


Transcript from the "Boolean Q&A" Lesson

>> Does anyone have any questions on all of this functioning goodness.
>> How did the fact answer work, because it's comparing Boolean with a string.
>> So, yes. So there is some small thing here that we kind of glossed over. Which is that I said that, in this slide, I said it should compare a guess string.

And then actually when we tried it out, we were comparing. We passed in, I'm on the wrong page. We passed in Booleans to isCorrect. So what happens if I pass in a string that says true? False, why?
>> Because they're not the same.
>> They're not the same, one's a Boolean, one is a string, right.

So if we have true string, well, if I spell it right, "true" === true. That's false. And in fact, even our loosey-goosey friend here would also agree. Sometimes it cares.
>> Preposterous.
>> So, if I were to pass in a string value of true.
>> My answer would have to be a string as well.

>> The answer would have to be a string as well. How can we convert something to a string that's not a string? So there's actually a hidden trick. A hidden spell in these wrapper objects that JavaScript makes, that we can actually call a method to string on pretty much any thing in JavaScript, That converts it to string.

So what we could do and I mentioned earlier. If folks had a real good ear, I mentioned that if you made true a Boolean. Or this the answer a Boolean true or false. As you probably did. Later, we might have to care whether it's a Boolean or a string.

So what we can do,is if we're expecting, and let's rename our variable here to guessString. It's another fun little thing IDEs can do, which is rename multiple things at the same time. If we're specifying that we're expecting a string here. We can actually make sure that we're comparing apples to apples, as it were, by converting fact.answer to a string with .toString.

So this is gonna come into play, because later when we pull out the thing that they clicked on and it value in html. We are dealing with strings all the time. So we noticed if we look at our button, I never defined it. If we look at our, what was it?

Option buttons zero. We noticed that its value is the string true. So later this is gonna come into play. So now if I try refreshing here with my new newly converted isCorrect, oops, is spelled incorrectly [LAUGH]. Okay, so our isCorrect, now we know it's the updated version. It's expecting a guest string.

If I now guess the string true, it works. Because it has converted my Boolean fact.answer to a string. And just to make sure that we're not losing it, isCorrect false string does say that it's false, cool? Okay, good question.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now