Using Conditional Statements
By admin | November 3, 2007
In JavaScript we have the following conditional statements:
- if statement – use this statement if you want to execute some code only if a specified condition is true
- if…else statement – use this statement if you want to execute some code if the condition is true and another code if the condition is false
- if…else if….else statement – use this statement if you want to select one of many blocks of code to be executed
- switch statement – use this statement if you want to select one of many blocks of code to be executed
In this session we will only use the if , else if , else
Using
i=0
if (i>10){ document.write("i is more than 10") }
else if (i=0)
{
document.write("i is equal to 0")
}
else {
document.write("i is lol")
}
In the above code first we declare a variable named ( i )
Then we check with the if statement that is i is greater then 10 if the statement is true
The browser will print
i is more than 10
If the statement is not true it goes to the else if
and the condition in the else if is checked and if it is true it will print
i is equal to 0
Now when if and else if both are false then the else statement gets executed and prints
i is lol
Thanks
krates
Topics: Javascript | Comments Off on Using Conditional Statements
Related Links: