JavaScript – Control Structures

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.

3 Types of Control Structures

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.

var num = 5; 
if (num < 10) { 
	console.log("Small"); 
} else { 
	console.log("Large"); 
}
//> "Small"

var number = 0;
while (number < 3) { 
	console.log(number); // Prints out the value
	number = number + 1; 
}
//> 0
//> 1
//> 2

var hello = function() { 
	console.log("World!"); 
};
hello(); 
//> "World"

There are three ways to change the control structures of a javascript program by using:

  • conditional statements (if-else)
  • looping control (while,do-while,for)
  • branch logic (functions)