JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes. JavaScript also supports functional programming — functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
In Javascript, a control structure, as the term implies, refers to the flow of execution of the program. There are two possible control structures: linear and nonlinear. The linear execution (also called sequential execution) refers to the execution of the statements in the order they are listed. In comparison, the non-linear execution refers to the execution of statements regardless of the order they are listed.
Loops of Execution
The loop control flow allows you to go back to a point in the program we were in before and repeat it with our current program state.
While Loop
An expression that starts with the keyword ‘while’ creates a loop. The word ‘while’ is followed by an expression in parentheses and then a statement. The loop keeps entering that statement as long as the expression produces a value that gives true when converted to Boolean.
var number = 0; while (number < 3) { console.log(number); // Prints out the value number = number + 1; } //> 0 //> 1 //> 2
Do-While Loop
A ‘do’ loop is a control structure similar to a ‘while’ loop. It differs only at one point: the ‘do’ loop always applies the body at least once and begins to test whether it should stop after the first run. To reflect this, the test appears after the body of the return.
var number = 3; do { console.log(number); number = number + 1; } while (number < 3) //> 3
For Loop
This loop is a slightly shorter and more comprehensive form of the most used algorithm of ‘while’ loop. The parentheses after a ‘for’ keyword must contain two semicolons. The part before the first semicolon initializes the loop, usually by defining a binding. The second part is the expression that checks whether the loop must continue. The final part updates the state of the loop after every iteration.
for (var number = 0; number < 3; number = number + 1) { console.log(number); } //> 0 //> 1 //> 2