DEV Community

Cover image for Loop js simplicity
Edeke Emmanuel
Edeke Emmanuel

Posted on • Updated on

Loop js simplicity

JavaScript loop

This is a mechanism or step to execute a piece of code repeatedly.

This usally have starting point and ending point or it could run continuously non stop(which could lead to memory loss). Its mostly occur in an array.
They are different types of loop, which initially perform the same task by repeating an action number of times(0-10000) or continuously.

Generally a loop should contain

  1. Initial expression(more like an initial value of a parameter)
  2. Condition (more like on what bases should you carry out a task)
  3. Increasing or descending order ( more like when the code has perform the first task, then rerun again e.g automating a dice game 100 times)
  4. Statement of execution (more like further adding features or certain conditions )

Types of loop JavaScript js

Loop js iterates a program or code.
Iteration is a single execution of a loop body.

1. do while loop:

This execute a task in an infinite number of times except the condition is false, the execution ends.

_In order to run multiple statement, block ({ }) is use to group them.

//statement of task
} while (//condition);

Enter fullscreen mode Exit fullscreen mode

Image description

2. while loop:

This is similar to do-while loop. If i++ is not included, it will loop forever, therefore causing storage issue in the browser.

Image description

Counter iterators:break & continue

This terminate loop and switch immediately.

Image description
Note: generally if the loop is being executed, it stop running if the condition is false contrary to the condition imply. Also if using prompt; when cancel is click, it end the program.

This omit the condition and restore the loop (do while, while, for).
It help restart the loop when another condition is met(more like rerun after a part in a condition is obtained and keep working).

Image description

Labelled statement:
This is a statement use to break or continue a loop. It helps to break or continue multiple nested loop at once.
It works two ways or both ways; clear example

Image description

3. for loop:

it's execute task fixed number of times.

Image description

4. for of loop:

This loops through the values of an iterable object and over iterable data structures such as arrays, strings, maps, nodeLists, and more.
Note: this help returns items with previous item when using increments.
Image description

This is a method in array that calls a function once for each array element.

Image description

5. for in loop:

This loop the properties of an object.

Image description

Review articles on function JavaScript

Beginner function
Advance function

Let us connect with each other

connection between two or more parties bring solution to a problem






Top comments (0)