If you need to add 2 Numbers you have to declare 2 variables and assign values and add them But a Question rises if we need to add 10 ,50 ,100 or 1000 numbers. It is very difficult to create 1000 numbers and assign them value .For This purpose we use Loops and there are many type of loops in Programming and we will discuse these loops according to the javascript. Four types of Loops are used in Javascript

  • While loop
  • do-while loop
  • for loop
  • for-in loop

while loop

While loop is used to repeate the block of code until a certain condition is met.Mostly we use while loop when we now the condition but we never know the number of iterations.For example when we want to count books untill a notebook in store.Then we never now how many block of code will run. we write while loop like this

var sum = 0;
var number = 1;
while (number <= 50) {
sum += number;
number++;
}

Here sum is our counter and number is our holder which hold value to compare with condition and While contain the condition .If condition is ture then code in side the while will run otherwise it will skip the while Number++ is call increament of holder .If we never have any holder increament then loop will be infinite loop(A loop which never ends).

do-while loop

If we want to run our code of once then we use do-while loop .In do-while holder is defined before the do and exit condition is define at end of do-while block.For example we want to search a number then we compare our number atleast one time if match then skip if not then compare again and again until we find our number.

var sum = 0;
var number = 1;
do {
sum += number;
number++;
} while (number <= 50);

Difference of do-while and while is that do-while loop is executed atleast once but while not execute if condition is false. In do-while condition is checked after one time compilation

for loop

Most used loop is for loop .for loop is consists of three important parts ,One is Initialization, Second is Conditions and Third is Increament and all are seprated by Samicolons. for loop is define as

var sum = 0;
for (var i = 1; i <= 50; i++) {
sum = sum + i;
}

In for-loop, all there are important and complusery. Initialization can be define before the for loop and increament can be written inside the loop But Condition can not be written any where else. e.g

var sum = 0;
var i = 1;
for (; i <= 50; ) {
sum = sum + i;
i++;
}

Here Samicolon must be written.Basic difference between for loop and a while Loop is that we use for loop when we know about numbers of itration and while loop is used when we never know about number of itrantions.

for-in loop

for-in is used for objects to access the properties of an object. Code in the loop will be executed once for each property.for-in is define as:

var person = {fname:"John", lname:"Doe", age:25};
for (x in person) {
txt += person[x];
}

break vs continue

Both break and continue are used to stop a loop but difference is that break stop the loop but continue skip remaining code of once. For example we have 100 people and we want to select only one so if we find our required person then we break the loop and if our condition is that we want count only those people who have age greater the 25 and if age is less then 25 than system no count.