 laika222
5/18/2017 - 3:38 PM

## Basic IF, IF - ELSE, and IF - ELSE IF - ELSE statements.

Basic IF, IF - ELSE, and IF - ELSE IF - ELSE statements.

``````<!-- Basic IF statement will have an IF statement (what to do if the test is true), and an ELSE statement (what to do if the test is not true). You can have multiple logical tests by using an IF statement, any number of ELSE IF statements, and an ELSE statment in case none of the IF or ELSE IF statements are true. -->

<script language="javascript" type="text/javascript">

var age = 18;

if (age <= 10)
{
document.write("You can't vote, you're not even a teenager<br/>");
}
else if (age < 18)
{
document.write("You can't vote, but you're getting closer to being old enough<br/>");
}
else
{
document.write("You CAN vote, yay!<br/>");
}

/*
Comparision Operators

==  Equivlency
=== Equivlent in value and type
1 == "1"  true
1 === "1" false
>   Greater Than
<   Less Than
>=  GT or =
<=  LT or =
!=  Not Equal To

&&  And  (used to combine two logical operators)
||  Or   (used to create an OR statment between two logical operators

*/

</script>

<!-- DETAIL!!! -->

<script language="javascript" type="text/javascript">

var age = 18;   // Creates variable age, sets it to a value of 18

if (age <= 10)  // First logical test, IF statement, if age variable is less than or equal to 10,
{
document.write("You can't vote, you're not even a teenager<br/>");   // Then write 'You can't vote, you're not even a teenager'
}
else if (age < 18)   // Second logical test, ELSE IF statement (you can have as many of these as you want), they are tested in order, if age viarable is less than 18 (but over 10 due to the fact that 10 and under was already tested in the first IF statement),
{
document.write("You can't vote, but you're getting closer to being old enough<br/>");   // Then write 'You can't vote, but you're getting closer to being old enough'
}
else   // ELSE, what to do if none of the previous logical tests are true,
{
document.write("You CAN vote, yay!<br/>");   // Then write, 'You CAN vote, yay!'
}

</script>``````