Last active
July 17, 2022 12:25
-
-
Save donbrae/5a4674e25765866f347cfb0e911f0c8c to your computer and use it in GitHub Desktop.
Basic looping in JavaScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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