Loop js simplicity
Tags: javascript, programming, career, webdev
Published: 2023-04-15 11:48:20

## 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._

<u>**Generally a loop should contain**</u> 
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.
_
```
do{
//statement of task
} while (//condition);

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s4ikawod1tkl0qweqs8.png)
## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2p37fad9xlxyx0kypu3.png)

## Counter iterators:break & continue 

**Break:**
This terminate loop and switch immediately.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz2qgfbpnqc6vmazr7af.png)
_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._

**Continue:**
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhf3vfnfivkh0vz9wdju.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfgtr0jiq14e14e3b8rf.png)
 

## 3. for loop: 
it's execute task fixed number of times.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/notik19q71z8o346ivvs.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2vkr0d18ghyf0yxinj2.png)

**forEach:**
This is a method in `array` that calls a function once for each array element.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jwzuwkq0qu5oky51l7e.png) 

## 5. for in loop:
This loop the properties of an object.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw9ocj74tz2ou2v156q1.png)


> ## Review articles on function JavaScript
> [Beginner function](https://dev.to/ebakecode/function-js-javascript-complete-guide-2ih1)
> [Advance function](https://dev.to/ebakecode/function-js-javascript-advance-principles-1je0)


## Let us connect with each other

> connection between two or more parties bring solution to a problem 

[Github](https://github.com/edekeemmanuel/)

[LinkedIn](https://www.linkedin.com/in/ebakecode/)

[Twitter](https://twitter.com/EBakeCode)

[Instagram](https://instagram.com/ebakecode)

[Facebook](https://facebook.com/ebakecode)