xxxxxxxxxx
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
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
let i;
for (i = 0; i < 6; ++i) {
console.log("Hello"); // print Hello 6 times
}
xxxxxxxxxx
// Sample Data:
const days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
// Samle Data which has undefined items:
const days = ["Monday","Tuesday",,"Thursday",,"Saturday","Sunday"]
// This Post might be a good read, to see a few caveats and advantages:
// https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea
//Classic For Loop using 3 expressions iterates all array indexes:
for (let i=0; i<days.length; i++){
console.log("Day: "+days[i])
}
//for..in iterates defined array indexes:
for (let day in days){
console.log("Day: ", days[day]);
}
//for..of iterates all array items:
for (let day of days){
console.log("Day: ", day);
}
//using array methods might be more suitable depending on the scenario:
days.forEach(function(day){
console.log("Day: ", day);
});
//available methods:
days.forEach()
days.map()
days.filter()
days.reduce()
days.every()
days.some() //this one is neat imo
xxxxxxxxxx
{
"type" : "record",
"name" : "Tree",
"fields" : [
{"name" : "children", "type" : ("type" : "array", "items": "Tree"}}
]}
xxxxxxxxxx
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
for (let i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
xxxxxxxxxx
var test = 0;
type 1;
while (test < 10) {
console.log(test)
test = test++
}
/* while (condition) {
our code
} */
return(
0
1
2
3
4
5
6
7
8
9) to the from (instant) * 'in console'
type 2;
for (var n = 0;n < 10; n++) {
console.log(n)
}
//note separate codes in for loops with ";"
/* for (Initial value before starting the loop;loop Condition;value after ending loop) {
our code
} */
return(
0
1
2
3
4
5
6
7
8
9) to the from (instant) * 'in console'