75% developed

Common JavaScript Manual/Do .. While and For .. in loops

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Do .. While[edit | edit source]

In this cycle, executing the code in it and then check the condition and if it is true then the loop is executed again, otherwise terminate the loop. Let's see example.

names = ["JavaScript","Python","Ruby"];
i = 0;
do{
  name = names[i];
  i++;
}while(print(name) || i < names.length);

Do .. while loop can be used if code inside a loop set some values and in condition you call some function.

For .. in[edit | edit source]

For .. in loop using if need iterate over all properties of object. For example.

obj = {name:"Artem",
       country:"Russia",
       interests:"JavaScript"};
for(i in obj){
  print(i + ':' + obj[i] + '\n')
}

In this code on every iteration to variable 'i' set value with key of property.


While and For loops · Break, continue, labels