JS - Loops

aigle-levant · September 4, 2024

Let us learn to rewind in JS…

Loops

For-of loop

This loop is handy if we’re iterating items in a list :

const spotifyTopTen = new Array(
    "Paint My Soul", "The Nights",
    "Send Them Off!", "Everlasting",
    "Good Grief", "Hangin'",
    "Soy Yo", "Walk",
    "Hey Brother", "My Blood"
);

for (let song of spotifyTopTen)
{
    console.log(song);
}

map() let’s you do stuff with the individual items of an array :

function toUpper(array)
{
    let string = array.toString();
    return string.toUpperCase();
}

const weightOfLiving = [
    "Or under the weight of living?",
    "You're under the weight of living",
    "Under the weight of living",
    "You are under the weight of living",
    "The weight of living",
    "The weight of living"
];

const mapping = weightOfLiving.map(toUpper);
console.log(mapping);

/*
[
  'OR UNDER THE WEIGHT OF LIVING?',
  "YOU'RE UNDER THE WEIGHT OF LIVING",
  'UNDER THE WEIGHT OF LIVING',
  'YOU ARE UNDER THE WEIGHT OF LIVING',
  'THE WEIGHT OF LIVING',
  'THE WEIGHT OF LIVING'
]
*/