Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 25, 2023 02:08
Show Gist options
  • Save code-boxx/a7d50b01e5f9e576f881618fac27f12d to your computer and use it in GitHub Desktop.
Save code-boxx/a7d50b01e5f9e576f881618fac27f12d to your computer and use it in GitHub Desktop.
Javascript Loops & Iterations

JAVASCRIPT LOOPS & ITERATIONS

https://code-boxx.com/javascript-loops/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) INCREMENT
for (let i=1; i<=3; i++) {
console.log(i); // 1, 2, 3
}
// (B) DECREMENT
for (let i=3; i>=1; i--) {
console.log(i); // 3, 2, 1
}
// (C) LOOP ARRAY
var arr = ["a", "b", "c"];
for (let i=0; i<arr.length; i++) {
console.log([i]); // 0, 1, 2
console.log(arr[i]); // a, b, c
}
// (D) WE CAN ACTUALLY OMIT ALL INIT & CONDITIONS
var i = 0;
for (;;) {
console.log(i); // 0, 1
i++;
if (i==2) { break; }
}
// (A) ITERATE OBJECT
var person = {
name : "Jon",
email : "jon@doe.com"
};
for (let key in person) {
console.log(key); // name, email
console.log(person[key]); // jon, jon@doe.com
}
// (B) ITERATE ARRAY
var animals = ["Birb", "Cate", "Doge"];
for (let i in animals) {
console.log(i); // 0, 1, 2
console.log(animals[i]); // birb, cate, doge
}
// (C) ITERATE STRING
var hello = "world";
for (let i in hello) {
console.log(i); // 0, 1, 2, 3, 4
}
/* (A) ITERATE OBJECT - ERROR, NOT ITERABLE
var obj = { "foo" : "bar" };
for (let value of obj) {
console.log(value);
}*/
// (B) ITERATE ARRAY
var animals = ["Birb", "Cate", "Doge"];
for (let value of animals) {
console.log(value); // birb, cate, doge
}
// (C) ITERATE STRING
var hello = "world";
for (let char of hello) {
console.log(char); // w, o, r, l, d
}
var animals = ["Birb", "Cate", "Doge"];
animals.forEach((value, key) => {
console.log(value); // birb, cate, doge
console.log(key); // 0, 1, 2
});
// (A) PERSON OBJECT
var person = {
"Name" : "Jon",
"Email" : "jon@doe.com"
};
// (B) LOOP OBJECT ENTRIES
for (let [key, value] of Object.entries(person)) {
console.log(key); // name, email
console.log(value); // jon, jon@doe.com
}
var animals = ["Birb", "Cate", "Doge"], i = 0;
while (i < animals.length) {
console.log(i); // 0, 1, 2
console.log(animals[i]); // birb, cate, doge
i++;
}
var animals = ["Birb", "Cate", "Doge"], i = 0;
do {
console.log(i); // 0, 1, 2
console.log(animals[i]); // birb, cate, doge
i++;
} while (i < animals.length);
let i = 0;
while (i>=0) {
// (A) CONTINUE - SKIP TO NEXT CYCLE
if (i==2) {
i+=2;
continue;
}
i++;
console.log(i); // 1, 2, 5, 6
// (B) BREAK - STOP CYCLE
if (i==6) { break; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment