Welcome to the twentieth Easy JavaScript tutorial, part of EasyProgramming.net. In this tutorial, we will take a look at a way you can implement logical workflows in your code by using conditional statements. The particular statement we'll be covering is the if...else statement.
The if...else statement will allow you to create custom workflows for your users by setting conditions. When a certain condition is met, a specific part of the code is executed, and if another condition is met, another part of the code is executed. This is one of the few times you can actually speak in pseudo code and actually make sense.
I would also recommend that you check out my "if" statement tutorial in C++ to get even more practice and a better understanding of how the statement works. The syntax is the same!
if...else
statement:if(condition is met){ execute this piece of code; } else if(this condition is met){ execute this piece of code; } else if(this condition is met){ execute this piece of code; } else { execute this piece of code if no other condition is met; }
To fork the fiddle and follow along: https://jsfiddle.net/easyjs/d9zpfkae/
Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!