Modern JavaScript Fundamentals

Boolean Values

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment explains boolean values and how they allow you to apply simple conditions.

Keywords

  • JavaScript
  • js
  • boolean

About this video

Author(s)
Laurence Svekis
First online
11 April 2020
DOI
https://doi.org/10.1007/978-1-4842-5847-7_12
Online ISBN
978-1-4842-5847-7
Publisher
Apress
Copyright information
© Laurence Svekis 2020

Video Transcript

In this lesson, we are going to be looking at Boolean values. And we have looked at these before. And this is either a true or a false, so there’s two options with Boolean values. And what Boolean values do is they allow us to apply simple conditions. So if something is true, then we can run a piece of code. And if it’s not true, or if it’s false, then we can run a piece of code. So that provides us a number of different options where we can apply conditions. And with the conditions, we can also use the quality operators, when we do a comparison between two different values, and there’s also relational operators, so that’s less than, greater than, less than and equal to, greater than and equal to, equal to, so we’re going to be outputting some of these Boolean output values are the various operators and the results of those operators into the console in this lesson.

We knew with Boolean values, so it can be either true or false. So let’s set one to true, and we’ll put value into the console. So we see that it gets a result of true. Also, let’s create another variable. And we’ll just call this one word, and then this can contain the word JavaScript. And this is going to give us an option to apply some logic.

So within the console itself, we’re going to ask our first condition. And we’re going to check to see if word is equal to– so two equal signs, not just one, because one is to assign the value and then two is to do the check to see if it’s equivalent– so we’re checking to see if word is equal to JavaScript. And in the console, we’re being returned back true. So if we’re checking to see if the word is equal to HTML, and we know this condition is going to be false. So it’s returning back false in the console. So we can check to see if a number value of 9 is equal to a number value of 9 we get a result of true.

We can also introduce the not equal to, so we see that the question is 9 is not equal to 9. And we know that 9 is equal to 9. Not equal to sign. And we see that word is not equal to HTML, so JavaScript is not equal to HTML, so that comes back true. Relational operators, so we’re going to check to see if 9 is greater than 10. So what do you think the answer to this is? This one is false. And I’ll copy this out a few other times, so check to see if 9 is less than 10. And that one is true.

And we’ll do one more. We’re going to see if 9 is less than or equal to 10. So that’s true as well. And we can also update this to be greater than or equal to 10, and that comes back as false.

So setting up a variable value, and we’ll just give this one number a string value of 5. We’re then going to check within the console, checking to see if num is equal to 5. So we’re checking to see if the string value of num 5– so, numeric string 5– is equal to the number 5, and it comes back true.

So with JavaScript, you can also do an absolute, checking to see the data type as well. So that’s the 3 equal sign. So you’re going to encounter that as well. And the best way to do these types of conditions is to do it using the data type, so that you know that you have an absolute value check. So that also includes the data type. And when we see that we’re comparing num to 5, we know that this is coming back false, because the data types are different. So saving that, and that comes back as true, so it’s not equal to 5. But if we only have the one equal sign, do you think this is going to be true or false? And if you said false, you are correct.

We are not doing an absolute check, so that’s not checking the data type. This is going back to what we learned earlier, where we can check the type of a value, and we can check to see what the type of num is. And this, of course, is going to be a string value. And whereas we can check to see what the type of 5 is, and this is going to return back a number value.

So go ahead and try this out. You can try it with some numbers. You can also create some variables in order to check to see what’s coming back true or false. And then we’re going to be making use of these in the upcoming lesson. So try this out.