Day 6 of JavaScript
Tags: webdev, javascript, beginners, tutorial
Published: 2024-05-01 08:15:41

Hey reader :) . Hope you are doing well. In the last post we have seen operators and conditionals in JavaScript. In this post we are going to learn about how loops are used in JavaScript. So let's get started.

First of all let's understand what loops exactly are-:
Loops are something that can execute block of code number of times.
Let's say if you want to print "Hello world" 100 times, so instead of writing print statement 100 times you can simply define a loop with print statement and this loop will repeat itself 100 times.

So now you have understood how useful loop is, now let's see how they are defined in JavaScript.

JavaScript supports different kinds of loops:

1. for loop
2. while loop
3. do-while loop
4. for-in loop

## 1. for loop ->
`for` loop is used to execute a code specific number of times.
Syntax -:
`for(expression 1;expression 2;expression 3){
     //code
}`
So here in the syntax you are seeing three expressions and each has a specific meaning. Let's understand them -:

- Expression 1 is **generally** for initialization of variable and is executed (one time) before the execution of the code block.

- Expression 2 defines the condition for loop i.e. under what circumstances the loop will run.When condition becomes False the loop terminates.

- Expression 3 is generally used to some manipulation on variables.It is executed (every time) after the code block has been executed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2g0bs2o239ewia3zs4s.png)
So here we can see that we have initialized a variable `i` and given a condition for looping and increment the variable every time the print statement is executed.
You can manipulate `for` loop according to your requirements.

## 2. while Loop ->
The `while` loop loops through a block of code as long as a specified condition is true.
Syntax -:
`while(condition){
     //code
}`
So here we can clearly see that the loop will execute the block of code till the condition is true.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrui5ltzu5w9d0z2zdjp.png)
So here we have intialized a variable and checked the condition for looping ,if the condition is true the block code executes otherwise the loop terminates. 

## 3. do-while Loop ->
The `do while` loop is a variant of the `while` loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax ->
`do{
    //code 
}while(condition);`
So this loop will always be executed atleast once and this is also called Exit controlled loop. As the condition is checked in end.

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

## 4. for-in Loop ->
The JavaScript for in statement loops through the properties of an Object. To get more clarity on this we will first see what object is and then we will see the use of `for-in` loop.

So this is it for this post in the next post we will read about functions in JavaScript .Till then stay connected and don't forget to like the post and follow me :)