Check out a free preview of the full Introduction to JavaScript course
The "Booleans" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Brian introduces booleans, which are how true and false are handled in JavaScript.
Transcript from the "Booleans" Lesson
[00:00:00]
>> Brian Holt: So I'm actually just gonna delete all of this,
>> Brian Holt: const isTheSkyBlue. Actually, it's kind of gray today if we're being honest. But well, let's just roll with blue for now. True, okay, and then I can just say console.log isTheSkyBlue. Refresh here, notice that it says false. [LAUGH] Just kidding, it says true.
[00:00:32]
So these are called Booleans. It's literally true or false, those are the only two options, and these are quite useful, we use them quite a bit in programming. Like imagine that you were making smart lights or something like that, you would have like are the lights on and that would be either true or false.
[00:00:49]
And you would flip that back and forth depending on whether or not the person had turned on the lights. Again, this is called a Boolean,
>> Brian Holt: Like that, that's how you spell it. I believe it's named after a computer scientist named Bool. Don't quote me on that though, it's been a long time since I was in school.
[00:01:15]
>> Brian Holt: And again, this can be either true or it can be false.
>> Brian Holt: So that's another one to keep in mind. Yeah, please.
>> Speaker 2: Does it have a default value if you didn't assign it to the thing?
>> Brian Holt: So like if I did this, one with const, you can't do that because const instance can never be reassigned, right?
[00:01:38]
But if I did let like this, it would be undefined because we haven't defined it. So the default value is undefined.
>> Brian Holt: Yeah, and then if you don't define it all it says isTheSkyBlue is not defined at all, so then it gets upset at you.
>> Speaker 3: Does it become a Boolean just because we are defining it as true?
[00:02:09]
>> Brian Holt: Mm-hm, yep. And that's actually a valid point to bring up. So JavaScript is what's called a dynamic language. So in other languages like, let's pick our favorite, like Java, right? It's a statically typed language, that means we have to define our types up front, which means if I say let myName = "Brian", right, this is now a string, right?
[00:02:38]
And then down here I can say myName = 15 now we're getting nonsensical but it doesn't matter. JavaScript doesn't care. It's like do whatever you want, I don't care, I will keep up, right? So if I say console.log(myName) right here, whereas if I try to do this in something like Java word, you have to maintain the types.
[00:02:57]
It would say note this was a string, you can only ever be strings, I don't accept numbers, right? JavaScript doesn't care.
>> Brian Holt: So when it dynamically moves from a string to a number to myName = true, to Boolean, it does not matter, it just keeps up with it.
[00:03:17]
>> Brian Holt: This makes it really easy to code because that's just one thought process you don't have to think about, but you get bugs from it. So it's a trade-off.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops