Conditional (ternary) operator
condition ? exprIfTrue : exprIfFalse
if (condition) {
// statement
} else if(condition){
// statement
}else{
// statement
}
If - Else Statements
if (condition) {
// what to do if condition is met
} else {
// what to do if condition is not met
}
if (condition) {
// what to do if condition is met
} else {
// what to do if condition is not met
}
const count = 20;
if (count === 0) {
console.log('There are no students');
} else if (count === 1) {
console.log('There is only one student');
} else {
console.log(`There are ${count} students`);
}
var init_value = 0;
if(init_value > 0){
return true;
} else {
return false;
}
if (condition) {
// your code here
} else if(condition){
// code to run if condition is true
}else{
// code to run if condition is false
}
/* Shorten if-else by ternary operator*/
const go = "yes"
let race = null
race = go === "yes" ? 'Green Light' : 'Red Light';