Free Ebook cover How to use Javascript in website development

How to use Javascript in website development

5

(2)

37 pages

Flow Control Structures in Javascript

Capítulo 5

Estimated reading time: 2 minutes

Audio Icon

Listen in audio

0:00 / 0:00

The flow control structures in Javascript are fundamental for the development of web sites. They allow the programmer to control the execution of the code, according to the established conditions. There are three flow control structures in Javascript: if/else, switch, and loops.

If/Else

The if/else structure is used to make decisions based on a condition. It allows code to perform one action if the condition is true and another action if the condition is false. The syntax is as follows:

if (condition) {
  // code to be executed if condition is true
} else {
  // code to be executed if condition is false
}

Switch

The switch is used when there are several conditions to be tested. It allows code to perform different actions based on the value of a variable. The syntax is as follows:

switch(variable) {
  case value1:
    // code to be executed if the variable equals value1
    break;
  case value2:
    // code to be executed if the variable equals value2
    break;
  default:
    // code to be executed if the variable is not equal to any of the previous values
    break;
}

Loops

Loops are used to execute the same block of code multiple times. There are two types of loops in Javascript: while and for.

While

The while is used when you don't know how many times the block of code will be executed. It executes the block of code as long as the condition is true. The syntax is as follows:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

while (condition) {
  // code to be executed while the condition is true
}

For

The for is used when you know how many times the block of code will be executed. It executes the block of code a specified number of times. The syntax is as follows:

for (initialization; condition; increment) {
  // code to be executed while the condition is true
}

In summary, flow control structures in Javascript are essential for web site development. They allow the programmer to control the execution of the code, according to the established conditions. It is important to know each one of them well in order to use them efficiently.

Now answer the exercise about the content:

_Which of the following flow control structures is used when there are several conditions to be tested and allows the code to perform different actions according to the value of a variable?

You are right! Congratulations, now go to the next page

You missed! Try again.

The switch statement is specifically designed to handle scenarios where several conditions need to be tested. It enables executing different actions based on the value of a single variable, making it an ideal choice when multiple potential values are involved.

Next chapter

Functions in Javascript

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.