Using JavaScript if else
It is one of the most used decision expressions in JavaScript, as in all programming languages. If the condition between the if brackets is true, the statement is executed. If the condition does not meet the else section, the statement in this section is executed.
Usage 1:
if(condition){
//expression to be executed;
}
Example: If you are over 18, you can get a driver's license. Make the application that prints.
var age=20;
if(age>=18){
alert("You can get a driver's license");
}
Usage 2:
if(condition){
//expression to be executed if the condition is true;
}else{
//expression to be executed if the condition is not true;
}
Example: If you are over 18, you can get a driver's license. If not, your age is not suitable for getting a driver's license and apply for the remaining years.
var age=10;
if(age>=18){
alert("You can get a driver's license");
}else{
alert(your age is not suitable for getting a driver's license);
alert(you can get a driver's license after 18-years+");
}
Usage 3:
if(condition1){
//expression to be executed if condition1 is true;
}
else if(condition2){
//expression to be executed if condition2 is true;
}else{
//expression to be executed if condition2 is not true;
}
Example: If the entered value is between 100.0 and negative, apply the application that gives separate warnings.
var x=5;
if (x > 100) {
alert("x value is greater than 100");
} else if (x > 0) {
alert("x value is greater than 0.");
} else {
alert("x is a negative number");
}
Usage 4:
if (condition1){
//expression 1
}
else if (condition2){
//expression 2
}
else if (condition3){
//expression 3
}
...
else{
//expressionN
}