How to use JavaScript for statement?

img

Introduction

For statement is used to repeat a certain section of statement or to repeat the execution of a particular data based on some pre-defined condition.

For statement is used to create a loop which will execute till the specified condition becomes true. The loop will terminate as soon as the condition becomes false.

By using for loop, you can avoid to write specific code more than once. So, it is time saving and also you need to write shorter code.

Types of loop in JavaScript

Javascript basically supports four different types of loop. All of them are different from each other in terms of usage and functionality. They are:

  1. for
  2. for/in
  3. while
  4. do/while

for Statement

Syntax of for Statement is:

for (Condition 1; Condition 2; Condition 3) 
{
    code block to be executed
}

Here Condition is also known as the statement.
Now let us try to understand how the above syntax works.
Condition 1: It executes before the code block or loop starts.
Basically it is used to initialize the loop counter variable.

Condition 2
It gets executed after the code block is executed once.
After that it checks value of counter variable value with the specified condition.
If condition becomes true, then loop will be executed otherwise not.

Condition 3
It is executed after condition becomes true.
Basically it is used to increment or decrement the value of counter variable.
break statement is used inside loop to break the execution of loop.
continue statement is used to skip the execution of loop for that particular value.

Now let us understand it by taking a code example:

var i;
for (i=1; i<=5; i++)
{
    document.writeln("Inside For");
}

Code explanation: In the above example value of ‘I’ is initialized as 1 for first time.
After that it checks the value of ‘I’ with condition that is "i<=5". If it’s true, then code inside loop will be executed.
This code or loop will get executed until the value of ‘i’ becomes greater than 5.

Output of the above code will be:

Inside For
Inside For
Inside For
Inside For
Inside For

for/in Statement

For/in statement is used to loop through the properties of an object.
The code inside the block will execute once for each of the property.
Now let us see the Syntax of for/in statement:

for (var in object) 
{
    code block to be executed
}

Here var is the variable which gets executed until all property of object are executed. Object is that specified object which you want to use.

Now let us try to understand it using a code example.

var person = {t:"For", e:"In", eg:"Example"}; 
var text = "";
var x;
for (x in person) 
{
    text += person[x] + " ";
}

Code explanation:

In the above code x is the variable and person is the object.
The above written code will get executed three times as property length of the object is three.

Output of the above code will be:
For In Example

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?