Conditional statements are used to perform different actions based on different conditions.
Use if to specify a block of code to be executed, if a specified condition is true.
if (condition) {
// block of code to be executed if the condition is true.
}
Use else to specify a block of code to be executed, if the same condition is false.
if (condition) {
// block of code to be executed if the condition is true.
} else {
// block of code to be executed if the condition is false.
}
Use else if to specify a new condition to test, if the first condition is false.
if (condition1) {
// block of code to be executed if condition1 is true.
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true.
} else {
// block of code to be executed if the condition1 is false and condition2 is false.
}
Use switch to specify many alternative blocks of code to be executed.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Loops can execute a block of code a number of times.
for - loops through a block of code a number of times.
for (statement 1; statement 2; statement 3) {
// code block to be executed.
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
for/in - loops through the properties of an object.
for (key in object) {
// code block to be executed.
}
for/of - loops through the values of an iterable object.
for (variable of iterable) {
// code block to be executed.
}
while - loops through a block of code while a specified condition is true.
while (condition) {
// code block to be executed.
}
do/while - also loops through a block of code while a specified condition is true.
do {
// code block to be executed.
}
while (condition);