Skip to content

Instantly share code, notes, and snippets.

@donbrae
Last active July 17, 2022 12:25
Show Gist options
  • Save donbrae/5a4674e25765866f347cfb0e911f0c8c to your computer and use it in GitHub Desktop.
Save donbrae/5a4674e25765866f347cfb0e911f0c8c to your computer and use it in GitHub Desktop.
Basic looping in JavaScript.
// Loop array
const foo = ["ane", "twa", "three"];
foo.forEach(item => console.log(item));
// Loop DOM elements
document.querySelectorAll('.foo').forEach(el => {
el.classList.add('bar'); // Add class, for example
});
// Loop object properties
const foo = {
derp: "merp",
herp: "berp"
};
Object.keys(foo).forEach(key => {
console.log(key, foo[key]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment