Alexa Metrics
RoBOOGarden Halloween Howler Event

JavaScript for beginners Loops in JavaScript

Carton image to show loops as an introduction to JavaScript

Definition of Loops:

Loops are control statements that execute a block of code again and again until a certain condition is met. They can also be used to manipulate data structures like arrays, lists, and more. JavaScript has many ways to implement loops, including while loops, do while loops, for loops, and for-in loops.

How a loop works:

When a “while” loop starts execution, the loop condition is first checked. If it happens to be true, the following statements (the Loop body) are executed and repeated until the condition becomes false. Checking the condition before entering the loop is called a pre-check.

When a while loop begins execution, the computer first checks the loop’s condition. If the condition is true, the statements included in the loop are executed and repeated until the condition becomes false. Checking the condition before entering the loop is called a pre-check.

A do-while loop is another variant of the while loop where the condition is appended at the end of the loop so that the loop is executed at least once before the condition is checked. Do-while loops are used when we want the body of the loop to be executed once because a certain variable must be updated before we run the checking condition. If the while condition is true when the loop has been run once, the body is re-executed until the condition becomes false. This is called post-check.

The for loop:

A for loop is another type of JavaScript loop. The syntax of the for loop comes in three parts. First is initialization, where the computer identifies the loop’s start point. Second is the condition, where the computer checks the current value of the loop's counter to see if it is still true or if the loop should end. Third is the increment, which tells the computer how much to increase or decrease the counter by.

Exiting the loop:

Break statements are widely-used in JavaScript to give programmers fine control of their loop . You use a break statement to exit a loop prematurely without executing the rest of its code. A continue statement does the opposite of a break statement terminating the execution of the current line of code and restarting the loop from the first line. When a continue statement is executed, the loop returns to a while test condition.

What is an infinite loop?

Sometimes we accidentally program what is called by an infinite loop , which occurs when a loop's condition will never be changed to false so that the loop can end. A famous example of this behavior is the while(1) loop, which will run forever. Loop like that are typically used in embedded systems that run the same firmware over and over again. For loops can be used to add delays to some systems by specifying the clock cycle speed of the processor and then seeing how many loop cycles will be equal to the time we need.

Are you interested in trying for yourself? Start learning JavaScript Journeys with Robogarden now.

JavaScript Series Introduction to JavaScript


Start JavaScript coding

Let's start JavaScript coding with RoboGarden learning journey.

Canada Flag Privacy Policy Terms of Use
Got an invite