js iteration code example
Example 1: boucle for javascript
var i=0;
for (i=0;i<=6;i++){
document.write("Le nombre est : " + i);
document.write("<br />");
}
Example 2: for of loop syntax javascript
for (let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log('Walking east one step');
}
Example 3: boucle for in js
let panier = ['fraise', 'banane', 'poire'];
for (const fruit in panier) {
console.log(panier[fruit]);
}
Example 4: javascript loops
JS Loops
for (before loop; condition for loop; execute after loop) {
// what to do during the 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(fruit);
console.log(panier.indexOf(fruit));
}