const iterable = [1, 2, 3, 4, 5]; // Using an array as an example

for (const item of iterable) {
    console.log(item);
}
Output in the console:
for (variable of iterable) {
    // code to execute for each iteration
}

const arr = ["apple", "banana", "cherry"];
for (const fruit of arr) {
    console.log(fruit);
}
// Output: "apple" "banana" "cherry"
const str = "abc";
for (const char of str) {
    console.log(char);
}
// Output: "a" "b" "c"
const map = new Map([["key1", "value1"], ["key2", "value2"]]);
for (const [key, value] of map) {
    console.log(key, value);
}
// Output: "key1 value1" "key2 value2"
const set = new Set([1, 2, 3]);
for (const value of set) {
    console.log(value);
}
// Output: 1 2 3
<ul>
    <li>Sushi</li>
    <li>Ramen</li>
    <li>Tacos</li>
</ul>
// Selecting all <li> elements
const liElements = document.getElementsByTagName("li"); 
console.log(liElements); // HTMLCollection(3) [li, li, li]

// Using for...of
for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Sushi" "Ramen" "Tacos"
<ul>
    <li>Sushi</li>
    <li>Ramen</li>
    <li>Tacos</li>
</ul>
// Selecting all <li> elements using querySelectorAll
const liElements = document.querySelectorAll("li"); 
console.log(liElements); // NodeList(3) [li, li, li]

// Using for...of
for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Sushi" "Ramen" "Tacos"
function showArgs() {
    for (const arg of arguments) {
        console.log(arg);
    }
}

showArgs(10, 20, 30);
// Output: 10 20 30
// break example: exit the loop if the number is 3 or greater
const numbers = [1, 2, 3, 4, 5];

for (const num of numbers) {
    if (num >= 3) break;
    console.log(num);
}
// Output: 1 2

// continue example: skip the number 3
for (const num of numbers) {
    if (num === 3) continue;
    console.log(num);
}
// Output: 1 2 4 5
const numbers = [10, 20, 30];

// Iterates over values only
for (const value of numbers) {
  console.log(value); // 10, 20, 30
}
const obj = {
    a: 1,
    b: 2
}

// This will throw an error!
for (const value of obj) {
  console.log(value);
}
// TypeError: obj is not iterable
const obj = {
    a: 1,
    b: 2
}

// Loop over keys
for (const key of Object.keys(obj)) {
  console.log(key); // a, b
}

// Loop over values
for (const value of Object.values(obj)) {
  console.log(value); // 1, 2
}

// Loop over both keys and values
for (const [key, value] of Object.entries(obj)) {
  console.log(key, value); // a 1, b 2
}