15: If and Else

You already learned about jumps in Exercise 11 where you wrote a function that caused your code to jump to the top of the function, and then return to where you called the function. If you don't remember this then please go back to Exercise 11 and study it again because we are going to use the concept of jumping in your code to explain an if-statement. Before you can learn about if-statements you must first learn about "testing", which involves comparing two values or variables to get a true or false answer.

If-Statements

The easiest way to talk about if-statements is to show you a small snippet of code and then show you how that relates to testing and jumps:

if (x === 10) {
   // first jump
} else if (x < 10) {
   // second jump
} else {
   // last jump
}

Learn JavaScript Today

Register today for the Beginner Basics course and get the new releases by just paying the difference. There's also special discounts for existing customers when new courses are released.


Still Not Sure? Check out more curriculum!