An if Statement Example.
if(number == 1){
document.writeln("The number is 1.")
}
if(number == 2){
document.writeln("The number is 2.")
}
if(number == 3){
document.writeln("The number is 3.")
}
if(number == 4){
document.writeln("The number is 4.")
}
if(number == 5){
document.writeln("The number is 5.")
}
if(number == 6){
document.writeln("The number is 6.")
}
if(number == 7){
document.writeln("The number is 7.")
}
if(number < 3){
document.writeln("The number is less than 3.")
}else{
document.writeln("The number is greater than or equal to 3.")
}
</SCRIPT>
You are just going to see the script that matches the condition.
These are TWO equal signs next to each other...
if(number == 7)
{
statements here
}
If Shortcut: (I show you this because you will see it all over the place, so you'd better know what it is!)
here's what it looks like:
if(number==7) document.write("number is 7")
What it means: if there is only one thing to do after your "if" statement,
you can skip the curly brackets
Just make sure to keep the whole thing on one line.
Another "if" shortcut: Conditional Operator
Originally:
if (dog == "bad")
{
var walk = "2 miles";
} else {
var walk = "1 mile";
}
In plain English, this means, "If the dog has been bad he hets a 2 mile walk. If he has been good, then he only needs a 1 mile walk."
With the conditional operator:
var dog= (behavior=="bad") ? "2 miles" : "1 mile";
Alternately,
if(dog=="bad") ? "2 miles" : "1 mile";