Loop
for
var cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
for(var i = 0; i < cars.length; i++)
console.log(cars[i])
for/in
get the index of the array or object
var cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
for (i in cars)
console.log(cars[i]);
var person = {fname:"John", lname:"Doe", age:25};
for (i in person)
console.log(i, person[i]);
for/of
get the element of the iterable such as Arrays, Strings, Maps, NodeLists, and more
var cars = ['BMW', 'Volvo', 'Mini'];
for (var i of cars)
console.log(i);
var txt = 'JavaScript';
for (var x of txt) {
document.write(x + "
");
}
while
var i = 0;
while (i < 10)
{
console.log(i);
i++;
}
do/while
var i = 0;
do {
console.log(i);
i++;
}
while (i < 10);
break
var cars = ['BMW', 'Volvo', 'Mini'];
for (var i in cars)
{
if (i == 1) break;
console.log(i, cars[i]);
}
continue
var cars = ['BMW', 'Volvo', 'Mini'];
for (var i in cars)
{
if (i == 1) continue;
console.log(i, cars[i]);
}
Label
outloop:
for (let i = 0; i < 3; i++)
{
innerloop:
for (let j = 0; j < 3; j++)
{
console.log(i, j);
if(i == 1) break innerloop;
if(i == 2) break outloop;
}
}
console.log("After loop ...");
outloop:
for (let i = 0; i < 3; i++)
{
innerloop:
for (let j = 0; j < 3; j++)
{
console.log(i, j);
if(i == 1) break innerloop;
if(i == 2) break outloop;
}
}
console.log("After loop ...");