Loading Please Wait...
JavaScript loops are used to execute a single block of code multiple times with different values. For example, working with arrays, strings, objects, etc.
JavaScript have different types of loops listed below:
The for statement creates a loop with 3 optional expressions.
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block. Normally used to initialize the first value.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed. Normally change the value to next step.
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
You can initiate many values in expression 1 separated by comma and can omit expression 1 like when your values are set before the loop starts.
for (let i = 0 , len = 5, text = ""; i < len; i++) {
text += "The number is " + i + "<br>";
}
// Omitting the expression 1
let j = 0;
for (; j < 5; j++) {
text2 += "The number is " + j + "<br>";
}
If expression 2 returns true, the loop will start over again. If it returns false, the loop will end.
If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser.
// Omitting the expression 1 and 2
let i = 0;
for (; ; i++) {
text += "The number is " + i + "<br>";
if(i === 5){ break; }
}
Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else. Expression 3 can also be omitted (like when you increment your values inside the loop).
// Omitting the expression 1, 2 and 3
let i = 0;
for (; ;) {
text += "The number is " + i + "<br>";
if(i === 5){ break; }
i++;
}
The while loop loops through a block of code as long as a specified condition is true.
while (condition) {
// code block to be executed
}
while (i < 10) {
text += "The number is " + i;
i++;
}
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. This guaranteed that the loop must execute the code block at least once.
do {
// code block to be executed
} while (condition);
do {
text += "The number is " + i;
i++;
} while (i < 10);
The for in statement loops through the properties (key index) of an Object/array.
The for in loop iterates over an object/array. In each iteration it returns a key that can be used to access the object/array.
The for in does not guaranteed to iterate object/array in index order. It may select key in random order. So it is better to use for, for of, or Array.forEach() loops
for (key in object) {
// code block to be executed
}
const numbers = [45, 4, 9, 16, 25];
let txt = "";
for (let x in numbers) {
txt += numbers[x];
}
The for of statement loops through the values of an iterable object/array.
The for of loop iterates over an object/array. In each iteration it returns the value.
for (variable of iterable) {
// code block to be executed
}
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x;
}
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved