2 - Javascript:Control Statements I - If...Else statements
If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
Syntax:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}
Example
<script type="text/javascript">
//If the time on your browser is less than 10,
//you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("Good morning!")
}
else
{
document.write("Good day!")
}
</script>
So let's experiment.