xxxxxxxxxx
for (i = startValue; i <= endValue; i++) {
// Before the loop: i is set to startValue
// After each iteration of the loop: i++ is executed
// The loop continues as long as i <= endValue is true
}
For Loop
xxxxxxxxxx
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Example of For Loop
xxxxxxxxxx
for(let i = 0; i < arr.length; i++){
// code block to be executed
}
For In Loop
xxxxxxxxxx
for (key in object) {
// code block to be executed
}
Example of For In Loop
xxxxxxxxxx
for (let x in arr) {
// code block to be executed
}
For Of Loop
xxxxxxxxxx
for (variable of iterable) {
// code block to be executed
}
Example of For Of Loop
xxxxxxxxxx
for (let x of arr) {
// code block to be executed
}
While Loop
xxxxxxxxxx
while (condition) {
// code block to be executed
}
Example of While Loop
xxxxxxxxxx
while (i < 10) {
// code block to be executed
i++;
}
xxxxxxxxxx
for (let index = 0; index < array.length; index++) {
const element = array[index];
}
xxxxxxxxxx
var nums = [1, 2, 3, 5];
for (var i = 0; i,nums.length; i++){
console.log(nums[i]);
}
xxxxxxxxxx
var arr = [1, 2, 3, 4];
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
};
xxxxxxxxxx
var listItem = [
{name: 'myName1', gender: 'male'},
{name: 'myName2', gender: 'female'},
{name: 'myName3', gender: 'male'},
{name: 'myName4', gender: 'female'},
]
for (const iterator of listItem) {
console.log(iterator.name+ ' and '+ iterator.gender);
}
xxxxxxxxxx
var sum = 0;
for (var i = 0; i < a.length; i++) {
sum + = a[i];
} // parsing an array
xxxxxxxxxx
var arr = [1, 2, 3, 4];
arr.forEach(function(element) {
console.log(element);
});