loop js JavaScript keynotes

loop js JavaScript keynotes

loop js functionality

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.

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

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

Break:

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 automatically.

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

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

for loop:

it's execute task fixed number of times.

Image description

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

forEach:

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

Image description

for in loop:

This loop the properties of an object.

Image description

Coding is not hard ? Yes it is not at all, time management and consistency is the key.