Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done! You have completed Exploring JavaScript Conditionals!
You have completed Exploring JavaScript Conditionals!
Preview
We'll look at two "if..then" statements in this video to remind ourselves how they work. In the videos that follow, we'll see how to reform these statements into different conditional patterns.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC] 0:00
Hello there, Andrew here. 0:04
JavaScript developer, lifelong learner, and teacher here at Treehouse. 0:05
I'd like to show you a few ways to represent decisions in your 0:10
JavaScript code. 0:14
You've already learned how to write an if, 0:16
else statement, to choose between one scenario and another. 0:18
This is called a conditional because it does one thing if a condition is true and 0:22
something else if it isn't. 0:26
The paths that's an if, else statement follows are called branches. 0:29
They branch the flow of your programs logic in different directions 0:34
based on the results of a conditional test. 0:39
You can branch off in as many directions as you want. 0:42
Let's look at two examples of branching, using if statements. 0:46
A two way and a multi way conditional. 0:50
We'll meet them in different ways later. 0:53
First let's look at a very simple example of if, else statements. 0:57
If you'd like to follow along, open up the workspace with this video or if you prefer 1:02
to use your own text editor, download the project files with this video. 1:07
I have a file named two.js, which is a simple example of two way branching. 1:11
I have another file named multi.js, with multi-way branching. 1:19
And another file short_circuit.js, with no code in right now. 1:24
I'll run these in node but 1:31
everything I'll show you in this workshop will work in the browser too. 1:32
In two.js, you see that I have a variable named is true which is set to true. 1:37
If the variable contains a true value the console prints yes, 1:44
if the value is false the console prints no. 1:50
Let's run this by typing node two.js and we see that yes it's printed. 1:56
Now if we change is true to false and 2:05
we rerun it we see no. 2:11
Let's take a look at multi.js now, we have a multi-way branch here. 2:15
If the day value is zero, then the console prints the string Sunday. 2:23
If it's one, Monday. 2:30
Two, Tuesday. 2:33
And so on. 2:37
If it's not a number between zero and 2:38
six invalid day will be printed to the console. 2:40
Let's see this in action. 2:45
Day is set to 1 now. 2:47
So when we type in node multi.js, Monday appears. 2:49
Let's change it to four and re-run it. 2:57
We see Thursday. 3:02
If we change it to new Date().getDay() 3:04
you should see today's day. 3:10
This may not be quite accurate if you're using workspaces 3:15
depending on the number of time zones between you and Treehouse servers. 3:19
If you run this locally on your computer you should get the correct day there. 3:23
Let's see our default case now. 3:28
Set day to 8. 3:30
And run the script. 3:35
We see Invalid Day. 3:37
Great. 3:41
We'll come back to these examples in the following videos and 3:42
rewrite them with different conditional statements. 3:45
The conditional statements we'll look at in this workshop 3:48
aren't necessarily considered best practices, but 3:51
you will see these pop up in stack overflow, tech blogs and other code bases. 3:54
So it's good to know about them understand them. 4:00
Our first stop on this tour is to visit the switch statement, 4:03
a very compact way of adding multiple branching logic to a program. 4:06
I will see you there 4:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up