javascript how to loop code example
Example 1: javascript for loop
for (var i; i < 10; i++) {
}
Example 2: javascript for
let str = "";
for (let i = 0; i < 9; i++) {
str = str + i;
}
console.log(str);
Example 3: javascript loop
let array = ["foo", "bar"]
let low = 0;
let high = array.length;
for(let i = low; i < high; i++) {
console.log(i);
console.log(array[i]);
}
Example 4: javascript loops
JS Loops
for (before loop; condition for loop; execute after loop) {
}
for
The most common way to create a loop in Javascript
while
Sets up conditions under which a loop executes
do while
Similar to the while loop, however, it executes at least once and performs a check at the end to
see if the condition is met to execute again
break
Used to stop and exit the cycle at certain conditions
continue
Skip parts of the cycle if certain conditions are met
Example 5: boucle for of javascript
let panier = ['fraise', 'banane', 'poire'];
for (const fruit of panier) {
console.log(panier.indexOf(fruit));
}