Tuesday, November 1, 2016

JavaScript Conditions: If/else Statement

      Conditional Statements are used to perform different actions for different decisions and situations. In JavaScript we use "if" to specify a block of code to be executed, if a specified condition or statement is true. "else" will be used  to specify a block of code to be executed, if the same condition from the "if" statement is false. "else if" will be used to specify a new condition to test, if the first condition is false and the condition of this statement is true, this statement will be executed before the "else" statement.

example:

if(expression){
code will execute if expression is true
}
else if (expression2){
code will execute expression is false and expression2 is true
}
else{
code executed when both expressions are false
}

      "switch" is similar to "if"/"else" statements. They have a default value in which codes executes when nothing happens. Then they have case values that can be as much as you want, which are executed if a case is true. 

example:

switch(expr){
case value1:
execute if expr==value1
break;
case value2:
execute if expr==value2
break;

default:
executed if no case values match the expression

}

No comments:

Post a Comment