|
Javascript
Conditions
Conditional statements are used to perform
different actions based on different conditions.
1. Condition if
IF
1. Can be used alone:
if (x > 0)
{
document.write("The number is positive");
}
With if (condition){ ...}, the condition is true.
2. Can be used alone with else:
if (x > 0)
{
document.write("The number is positive");
}
else
{
document.write("The number is negative");
}
3. Many "if" and many "else":
if (x > 0)
{
document.write("The number is positive");
}
else if (x < 0)
{
document.write("The number is negative");
}
else if (x == 0)
{
document.write("The number is null");
}
else
{
document.write("The variable is not a number! .. ");
}
2. Switch:
The syntax is:
switch(x)
{
case a:
execute code1
break;
case b:
execute code2
break;
default:
other if x is
different from case a and a
}
The value of "x" can be a bumber or a string.
|