Control Flow in JavaScript 


Control Flow in JavaScript 

Control Flow is the technical term for specifying the sequence in which a code executes. In this article, we will deeply discuss the three structures that influence the control flow.

What is Control Flow? 

Have you ever wondered what goes on behind the scenes while a program is running? Of course, most of us know that you will have to write lines of code to achieve the desired output. However, not everyone knows the answer to ‘How?’.

Originally, code executes in a top-down manner, unless the code control changes its course when it meets with other structures such as: 

  1. Loops
  2. Conditionals
  3. Functions

Below, we will discuss what each of these control flow structures is. 


In computer programming, loops are a conditional statement that repeats the given task until the given condition is met. Loop statements are also known as iteration statements.

There are three types of loops

  • For 
  • While
  • Do while
  • For Loop

The for loop is one of the most commonly used loop structures in most programming languages, not just JavaScript. It is also simple to use.

Until the provided condition is met, the for loop executes blocks of code. So let’s have a look at the syntax in more detail:

Syntax of For loop:

for (initialExpression; condition; updateExpression) {
    // for loop body


  • For refers to the for conditional loop statement
  • The initial variable from which the execution process begins is referred to as initialExpression.
  • Condition is where the conditions are written.
  • The initial variable is either increased or decreased in the updateExpression.

While executing, the control moves to the updateExpression and gets updated and then moves to the condition. If the updated variable is true, the process continues till it reaches false.

  • While Loop

In terms of execution, there is not much of a difference between a while loop and for loop. While loop is similar to for loop, in the fashion that it repeats the process until the conditions are met.

Syntax of while loop:

while (condition) {
    // body of loop


  • The while refers to the while conditional statement.
  • Condition refers to the condition which is meant to be met.

The initial variable is given before the while statement. The execution ends when the conditions are met.

  • Do-while Loop

Like how the name is similar, the concept of do-while loop is similar to while loop. Except, like the traditional way of looping, the do-while loop executes the statement first then checks the condition.

Syntax of Do-while loop:

// body of loop
} while (condition)


  • The do represents that the loop structure is of do while.
  • The condition is where the control checks if the variable fulfils it.


Conditional statements are decision making statements. In programming, developers will have to deal with multiple hypothetical scenarios. However, if you are going to write codes that execute the correct output, you will end up with the programmer writing tons of codes or, worse, none at all.

To avoid such scenarios, we use conditional statements. In a conditional statement, the programmer writes several codes which will be executed if the condition is met. If the condition is not met, then another statement is executed. 

Sounds a lot better than writing a thousand lines of code?

The most common conditional statements used in JavaScript are:

  • If 
  • Else if
  • if…else
  • Switch
  • If Statement

The if statement is the fundamental conditional statement. It comes with one condition and a block of statements within it. 

Syntax of if statement:

if (expression) {
   Statement(s) // if the expression is true

If the condition is true, the code is executed. If not, then the control moves on to the following code.

  • If…else Statement

The if…else statement also is similar to if, with a twist. Instead of the control moving on to another line after the condition is proved, the given else statement is executed.

Syntax of if…else statement:

if (expression) {
   Statement(s) // if the expression is true
} else {
   Statement(s) // if expression is false

The first statement is released when the condition is proved; if not, the else statement is executed. Then the control moves on to the following code.

  • Else if Statement

The else if statement is very similar to if..else, but more practical. The mechanism of else it is given through the syntax below:

Syntax of else if Statement:

if (expression 1) {
   Statement(s) // if expression 1 is true
} else if (expression 2) {
   Statement(s) // if expression 2 is true
} else if (expression 3) {
   Statement(s) // if expression 3 is true
} else {
   Statement(s) // if no expression is true

In contrast to if-else, we can include a variety of conditions in else if. The control checks the expression from top to bottom, goes to the next if one is false, and eventually finishes with the else statement, which acts as a default statement when none of the conditions are met.

  • Switch Statement 

The switch statement looks a lot like an if statement; however, unlike if and else if, which check the condition on each line, the switch tests the condition once and then performs the relevant expression. A default statement is released if the condition isn’t met.

Syntax of switch statement:

switch (expression) {
   case condition 1: statement(s)
   case condition 2: statement(s)
   case condition n: statement(s)
   default: statement(s) // executed when none of the conditions are met

The break statement after every case statement is to let the control know the end of the statement. If the break is not added, the control will end up executing every statement. 


When you write a specific block of code for a certain purpose, that code will be needed in various places of the code, which is a typical programming issue. Therefore, writing the same code repeatedly is not a wise option, which is why functions exist.

Functions eliminate the need for repeating the same code. All you will need to do is write a function then use it whenever you want at any part of the program.

Syntax of Functions:

<script type = "text/JavaScript">
      function functionname() {

To use or invoke the function block in a different part of the program, you can simply call the function.


In JavaScript, the control flow is fluid; the only times it is disrupted are when the statements mentioned above are used. 

Control statements are quite helpful, but programmers must use them carefully and in accordance with proper syntax to prevent complications. We believe you have a better understanding of what control statements are.

Learn some coding basics for free

If you want to learn some of the basics of software development for free, try this free 5 Day Coding Challenge. On it, you will learn the basics of HTMLCSS and JavaScript. It takes just one hour a day over five days. Register now through the form below. Alternatively, if you want to learn full stack software development, you can read more about our programme here.

Coding the Future: Developer Spotlight - Melissa Buckingham

In March 2023, Melissa Buckingham joined Level 5 Diploma in Web Application Development programme, delivered at North East Surrey College of Technology (NESCOT) by Jonathan Jacobsen using the Code Institute learning platform. The course equips learners with full-stack software development skills, making them ready to enter the digital workforce in various roles. We asked Melissa’s […]

Code Institute’s €250,000 Spring Scholarship Fund - Education for All

The world is full of opportunities, and with technologies advancing and AI becoming the keyword for work success, there really hasn’t been a time more suited to people who want and need to upskill for the future of work. However, many of us know that upskilling can be expensive. To combat this, Code Institute has […]

Coding the Future: Developer Spotlight - Emma Hewson

In October 2022, Emma Hewson joined Code Institute’s Level 5 Diploma in Web Application Development programme, which is run in association with Cardiff and Vale College and is funded via the Welsh Government’s Personal Learning Accounts (PLA) initiative. The course aims to equip learners in full-stack software development, preparing them to enter the digital workforce […]