Workflow

if ... else ...

// if(<condition>) <statement1>
if(x==y) x++;

// if(<condition>) <statement1> else <statement2>
if(x==y) x++; else x--;

if(x==y) x++; else if(x!=y) x--;

The if statement provides code execution by condition. If condition true, then statement1 will be executed. Otherwise statement2 will be executed from else block. The else block can be omitted.

statement1 and statement2 can be represented as group of statements in curly brackets.

switch

/*
switch (<expression>) {
  case <value1>:
    <sequence_of_statements1>
    break;
  ...
  case valueN:
       <sequence_of_statementsN>
    break;

  default:
  <sequence_of_statementsN>
}*/
var todayIs="";

switch (new Date().getDay()) 
   {
    case 0:
        todayIs = "non working day";
        break;
    
    case 1: case 2:
    case 3: case 4: case 5:
        todayIs = "working day";    
        break;

    case 6:
        todayIs = "non working day";   
        x = 23;
        break;

    default:
        todayIs="such day not exist";    
        break;
}

The switch statement is used to perform different actions based on different conditions.

The switch expression is evaluated one time. So it is more efficiently than repeated if statements.

The switch statement uses strict comparisons (===), so result ​​of the expression and values must be of the same type. Usually used integer values or strings.

The break keyword ends the switch block. If it is omitted, next case will be executed.

If no cases match, the default code block is executed. If default is omitted, then nothing executed. Usually the default block is placed at the end of switch.

for

/*
for (<init_statement> ; <cond_statement> ; <it_statement>) {
  // code block to be executed
}
*/

for(ind=5; ind<10; ind++) console.log("ind="+ind);

for(ind=5; ind<10; ind++){
     console.log("ind="+ind);
     ...
}

// infinite loop, use break in the executed code block for exit from loop
for(;;){
...
}

init_statement is executed one time before the execution of the code block.

cond_statement defines the condition for executing the code block.

it_statement is executed every time after the code block has been executed.

init_statement, cond_statement, it_statement could be omitted.

for ... in

/* 
for(<var> in <object>){

}
*/

var person = {fname: "Harry", lname: "Potter", age: 10};
var text = "", x;

for (x in person) {
  text += person[x];
}

The for ... in allows iterate over object properties.

for ... of

/* 
for(<var> of <iterable>){
// code block to be executed
}
*/

var digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var x, str = "";

for (x of str) {
  str +=  x + " ";
}

The for ... of allows iterate over any iterable object such arrays, strings, maps, sets and etc.

while

/* 
while(<condition>){
// code block to be executed
}
*/
var ind=0;
while(ind<10) ind++;

while(ind<20){
...
ind++;
}

// infinite loop, use break in the executed code block for exit from loop
while(true) {

}

The while loop executes code block while condition is true.

do ... while

/* 
do {
// code block to be executed
}while(<condition>)
*/

The do ... while loop executes code block while condition is true. Unlike while loop code is executed at least once whether condition is true or false.