Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Every condition is really just a true or false test. At the heart of this is a type of JavaScript value known as a Boolean value. Learn what Boolean values are and how to create them in JavaScript.
-
0:00
A condition in a conditional statement is really just a question with a yes or
-
0:04
no answer.
-
0:06
In JavaScript, we say that the condition evaluates to either true or false.
-
0:11
[SOUND] True and false have specific meaning in JavaScript.
-
0:15
They're called Booleans and along with strings and
-
0:18
numbers, they represent a third type of value in JavaScript.
-
0:22
Though they have an unusual name, Booleans are very simple.
-
0:25
In fact, a Boolean value can be only one of two possible values, true or false.
-
0:32
That's it.
-
0:33
Let's look at Boolean values in action.
-
0:36
You don't need to follow along in Workspaces for this.
-
0:38
I'm just going to use workspace to demonstrate these concepts.
-
0:41
You'll be doing plenty of programing in just a minute.
-
0:44
[BLANK_AUDIO]
-
0:47
In JavaScript programming, the two possible Boolean values are represented by
-
0:52
the keywords true and false.
-
0:54
Because all conditions return a true or false value,
-
0:57
we can actually see how these two keywords work by using them in this code.
-
1:02
If I type true inside the condition and
-
1:05
preview it, The condition is true is printed to the screen.
-
1:11
Now, if I type false inside the condition and preview it,
-
1:18
The condition is false is printed to the screen.
-
1:22
Notice that there are no quote marks around true or false.
-
1:25
They aren't strings.
-
1:26
They each represent a specific type of value,
-
1:29
a Boolean value, so don't put quotes around them.
-
1:33
You can assign a Boolean value to a variable just like you
-
1:36
can assign a string or number to a variable.
-
1:39
This is a common way to keep track of a certain condition throughout a program.
-
1:43
For example, let's see if we can use a Boolean value
-
1:46
in the random number guessing game.
-
1:49
You can follow along in Workspaces by opening the workspace on this page.
-
1:53
Open the script.js file.
-
1:57
Here's the program you created in the last video.
-
2:01
First, I'll add a new variable named correctGuess and
-
2:04
set it to the Boolean value false.
-
2:08
When the game begins, the player hasn't yet
-
2:10
guessed anything, so we'll say the guess is false.
-
2:14
The value of this variable will only change if
-
2:16
the player gets the correct answer.
-
2:18
So, next I'll remove the else clause here and
-
2:22
change the correct condition code block to set the variable to true.
-
2:28
So if the player guesses the correct number,
-
2:30
the correctGuess variable changes to true.
-
2:33
If the player guesses wrong, the variable isn't changed.
-
2:36
It stays what it was set at at the beginning, to false.
-
2:41
We can then test that variable and print out a message that matches the condition.
-
2:45
[BLANK_AUDIO]
-
2:53
This is a lot of code I know, but
-
2:54
this is a second conditional statement that we've added to the quiz.
-
2:58
We now have two conditional statements.
-
3:01
The second one checks to see if the value in the correctGuess variable is true.
-
3:06
If it is, then it writes a message telling the player he was correct.
-
3:10
If not, then a different message is printed,
-
3:12
telling the player what the random number was.
-
3:16
You can simplify this code a bit.
-
3:18
Remember that all conditions are either true or false.
-
3:21
That is, the test condition produces a Boolean value.
-
3:24
Because we're already storing either true or false in the correctGuess variable,
-
3:30
we don't need to use the equality operator at all.
-
3:32
We can get rid of it and just leave the Boolean variable there.
-
3:37
Let's step through this code line by line.
-
3:41
First, we create a variable with the Boolean false value.
-
3:44
Next, we create a random number.
-
3:46
We open a dialogue box and ask the user to supply a number.
-
3:51
Then the program tests the user's response.
-
3:54
If the user supplied the same number as the random number, then the correctGuess
-
3:58
variable is changed to true, meaning the player correctly guessed the answer.
-
4:04
If the player didn't, however, then the variable stays the same, false.
-
4:09
Then we have a new conditional statement.
-
4:11
If the value in correctGuess is true, then we write a winning message.
-
4:15
However, if the value is false, then we'll tell the player the number.
-
4:19
Let's see it in action.
-
4:21
Now, you might be wondering why do we go to all this bother.
-
4:25
The program worked just fine before adding this new variable.
-
4:28
Well, first it gave us a chance to start using Boolean values in our program.
-
4:33
But more importantly, it's going to make the program more flexible and
-
4:36
it'll let us respond to multiple conditional outcomes.
-
4:40
This is something you'll learn about in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up