Conditional statements are used to do various actions in response to certain conditions.
In JavaScript, there are three types of if statements.
The if statement is executed when a condition is true.
if (condition) {
// if the condition is true, a block of code will be performed
}
var marks=86;
if(marks > 80 ){
document.write("A Grade");
}
// Output - A Grade
The if statement is executed when a condition is true or else statement will be executed
if (condition) {
// if the condition is true
}else{
// if the condition is false
}
var marks=77;
if(marks > 80 ){
document.write("A Grade");
}else{
document.write("B Grade");
}
// Output - B Grade
It only analyzes the content if one or more expressions are true.
if (condition 1) {
// if the condition matches
}else if (condition 2){
// if the condition is matches
}else{
// if the condition is false
}
var marks=30;
if(marks > 80 ){
document.write("A Grade");
}
else if(marks > 40 ){
document.write("B Grade");
}
else{
document.write("Fail");
}
// Output - Fail