For
The easiest form of a loop is the for statement. This one has a syntax that is similar to an if statement, but with more options:
for (condition; end condition; change) {
// do it, do it now
}Let's see how to execute the same code ten-times using a for loop:
for (let i = 0; i < 10; i = i + 1) {
// do this code ten-times
}Note:
i = i + 1can be writteni++.
To loop through the properties of an object or an array for in loop can also be used.
for (key in object) {
// code block to be executed
}Examples of for in loop for an object and array is shown below:
const person = {fname:"John", lname:"Doe", age:25};
let info = "";
for (let x in person) {
info += person[x];
}
// Result: info = "JohnDoe25"
const numbers = [45, 4, 9, 16, 25];
let txt = "";
for (let x in numbers) {
txt += numbers[x];
}
// Result: txt = '45491625'The value of iterable objects such as Arrays, Strings, Maps, NodeLists can be looped using for of statement.
let language = "JavaScript";
let text = "";
for (let x of language) {
text += x;
}Last updated
Was this helpful?