course content

Course Content

Introduction to JavaScript

whilewhile

Now let's take a look at loops.

A loop is a structure that executes a set of instructions repeatedly while a certain condition is true.

There are three types of loops in JavaScript:

  • while;
  • do-while;
  • for.

Let's take a closer look at each one.

while

The while is the most simple loop. This operator repeats the code block while the condition is true.

To create this loop, you need to use the while keyword with condition in parentheses (()) and code block in curly brackets ({}). This syntax is similar to the if syntax:

Look at the example:

The interpreter checks the condition and executes the code block if the condition is true. These actions repeat until the condition is false:

Note

Be careful with the condition! If the condition always evaluates to true, the loop will never break and will run indefinitely. Such moments are called infinity loops.

1. How many times will the program output "Hello!"?
2. How many times will the program output "JavaScript"?
3. How many times will the program output "Be careful!"

question-icon

How many times will the program output "Hello!"?

Select the correct answer

question-icon

How many times will the program output "JavaScript"?

Select the correct answer

question-icon

How many times will the program output "Be careful!"

Select the correct answer

Section 5.

Chapter 1