One of the most commonly used conditional statements in all programming languages are if and else statements. They can be very basic and also very complex, depending on the type of conditions you are trying to evaluate. Below you can see a conditional statement been used to evaluate if something is either true or false, yes or no etc.
<script type="text/javascript">
var age = 18; // assign age variable a value
// Then we start our condition, if age is greater than or equal to 18
if (age >= 18) {
document.write("You are an adult");
} else { document.write("you are still under 18");
}
//Then we wright to the page depending on weather it evaluated to true or false
</script>
You can see here in the above example that we either have two choices people who are 18 and above as adults, and people who are under 18. We can take this further though by using an if else statement as shown below.
<script type="text/javascript">
//assign variables there values
var ben = 21;
var james = 24;
// declare condition "if ben is greater than 18 and james is greater then 18"
//if the condition evaluates true then we will write this to the page if it evaluates false we will move on to the next condition to be evaluated.
if ( ben >= 18 && james < 18) {
document.write("Ben is an adult and james is under 18");
} else if (ben < 18 && james >= 18) {
document.write("Ben is under 18 and james is an adult");
}else if (ben >= 18 && james >= 18) {
document.write("Ben and james are both adults");
}else { document.write("Ben and james are both under 18"); }
</script>
You can see above that there are four different conditions which could evaluate true. If the statement evaluates false it falls through to the next condition otherwise it write's the string of text for that statement to the page. I have put some comments in the code to help you understand what is happening. You can see here that this could be used in many different circumstances and gives you the basics to go and try this out yourself.