Javascript : If Else
If
if (isActive === true) {
/// ...code
}
Short Circuit
When you have 2 conditions with AND operator, and the first condition is false, js will not even bother to look at the second conditions, it just moves on.
if (isActive === true && isNew === true) {
/// ...code
}
If isActive here is false, js wont even bother checking isNew.
If Else
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Ternary
This is a shortcut code.
var isValid = ("<% =isValid %>".toLowerCase() === "true") ? true : false;
It is equivalent to this:
var isValid = function() {
if ("<% =isValid %>".toLowerCase() === "true") {
return true;
} else {
return false;
}
}
switch
switch (robsInt) {
case 0:
number = "Its a zero";
break;
case 1:
day = "I'm a number 1";
break;
default:
text: "Default";
}