
if -else statementThe else statement is executed when the ifstatement condition is false.
if (condition) {
// statements
} else {
// statements
}
else if statementTo be able to chain multiple conditions, we useelse ifstatements. The else if statement is executed when the previous if or else if statement condition is false.
if (condition1) {
// statements
} else if (condition2) {
// statements
} else if (condition3) {
// statements
} else {
// if all above turn to be false,
// this block of code will be executed
}
โ๏ธ One very important syntax rule to follow when writing your
if, else ifandelsestatements is:
ifandelse ifstatements always require a condition in order to work,elsestatements do not need a condition, as it means to run the block of code in every other possible case.
SwitchThe value of the variable pet is actually โdogโ, so it will find if โdogโ is one of the cases in the switch block and execute the statement inside.
JSResult Skip Results Iframe
EDIT ON
let pet = "dog";
switch (pet) {
case "cat":
console.log("I will be break a lot of things but hey, I'm adorable ... ")
break;
case "dog":
console.log("I will be very annoying but I'm your best friend ... ")
break;
case "hamster":
console.log("I will be impossible to find but I'm reeeeaaaally cute ... ")
break;
default:
console.log("I'm a special pet!")
break;
}
break break statement finshes a block execution such as conditionl blocks or loop blocks, and continues with the execution of the program. break very rarelym because you should be able to control the flow of your program instead of break in it.Switch break cheatsheet