Skip to content
the poor coder

looping of javascript

Poor Man
Published
Reading time
2 min read
On this page

Looping of Javascript#

If you are a beginner in Javascript, you might have heard the term "looping" quite often. Looping is a process of executing a set of instructions repeatedly while a certain condition is true. In Javascript, there are mainly two types of loop statements - "for" loop and "while" loop. Let's take a closer look at each of them.

The For Loop#

The "for" loop is used when you know exactly how many times you want to execute a set of instructions. Here is the basic syntax of a "for" loop:

Text

for (initialization; condition; increment/decrement) {
  // code to be executed
}

The "initialization" part is executed once at the beginning of the loop. It usually declares and initializes a variable that will be used in the loop. The "condition" part is tested before each iteration of the loop. If it evaluates to true, the loop will continue. If it evaluates to false, the loop will stop. The "increment/decrement" part is executed after each iteration of the loop. It usually increments or decrements the variable declared in the "initialization" part.

Let's see an example:

Text

for (var i = 0; i < 5; i++) {
  console.log(i);
}

This "for" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

The While Loop#

The "while" loop is used when you don't know exactly how many times you want to execute a set of instructions. Here is the basic syntax of a "while" loop:

Text

while (condition) {
  // code to be executed
}

The "condition" part is tested before each iteration of the loop. If it evaluates to true, the loop will continue. If it evaluates to false, the loop will stop.

Let's see an example:

Text

var i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

This "while" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

The Do...While Loop#

The "do...while" loop is similar to the "while" loop, but the code inside the loop will always execute at least once, even if the condition is false. Here is the basic syntax of a "do...while" loop:

Text

do {
  // code to be executed
} while (condition);

Let's see an example:

Text

var i = 0;
do {
  console.log(i);
  i++;
} while (i < 5);

This "do...while" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

Conclusion#

Looping is a fundamental concept in programming, and Javascript provides different types of loop statements to help you execute a set of instructions repeatedly. The "for" loop is used when you know exactly how many times you want to execute the instructions, the "while" loop is used when you don't know exactly how many times you want to execute the instructions, and the "do...while" loop is similar to the "while" loop but executes the code inside the loop at least once. By using these loop statements, you can make your code more efficient and flexible.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC