Conditionals

Conditional statements are used to run units of code according to specific conditions.

if (something is true) {

  then this line of code happens;

} else {

  if not true then this line of code happens;

}

Notice how the curly brackets surround the statement instead of being within the statement. In these cases, the curly brackets do not need a semicolon at the end. They are being used to contain the statements. The interpreter knows to look for the closing curly bracket after the type of unit is recognized, so a semicolon at the end is not necessary for a conditional statement.

if and else

switch

Loops

Loops are conditional statements that repeat a unit of code a conditional number of times.

for (i = 0; i < 10; i++) {

console.log(i);

}

More on loops later.

Next