Nov
23
2015
By abernal
While programming, most of the time a decision needs to be made, therefore we have the conditional tools, that help ups model such decisions
If and Else
Syntax
if (*some condition is true*){
// do this code
} else if (*other condition*) {
// Otherwise, do this code if match the other condition
} else {
// Otherwise, do this code
}
Sample
var valueA = 1;
var valueB = 10;
if (valueA > valueB){
console.log("The "+valueA+" is greater than "+valueB);
}else if (valueA == valueB){
console.log("The "+valueA+" is aqual to : "+valueB);
} else{
console.log("The "+valueA+" is minor than "+valueB);
}
Binary AND &&
Evaluates to true if both values at the left and right are true, otherwise will evaluate to false
Sample
var valueA = true;
var valueB = false;
if(valueA && valueB){
console.log("Both values are true");
}else{
console.log("");
}Binary OR ||
Evaluates to true if any of the two values at the left and right are true.
Sample
var valueA = true;
var valueB = false;
if(valueA || valueB){
console.log("Any Both values are true");
}else{
console.log("Both values are false");
}