Conditional Statements
JavaScript we have the following conditional statements:
if
else
else if
switch
The if Statement
Syntax
if (condition){
block of code to be executed if the condition is true
}Example :<hmtl><script>var age=20;if(age>18){document.write("Qualifies for driving");}</scritp></html>The else Statement
if (condition){
block of code to be executed if the condition is true
}else{
block of code to be executed if the condition is false
}Example :<hmtl><script>var age=20;if(age>18){document.write("Qualifies for driving");}else{document.write("doesnt qulify for driving");}</scritp></html>The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1){
block of code to be executed if condition1 is true
}else if (condition2){
block of code to be executed if the condition1 is false and condition2 is true
}else{
block of code to be executed if the condition1 is false and condition2 is false
}Example:Simillar to if else.The JavaScript Switch Statement
Syntaxswitch(expression){
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
No comments:
Post a Comment