How to iterate over a JavaScript object?

For most objects, use for .. in : With ES6, if you need both keys and values simultaneously, do To avoid logging inherited properties, check with hasOwnProperty : You don’t need to check hasOwnProperty when iterating on keys if you’re using a simple object (for example one you made yourself with {}). This MDN documentation explains more generally how to deal with objects … Read more

For-each over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) a simple old-fashioned for loop – async-friendly (rarely) for-in with safeguards – async-friendly Some quick “don’t”s: Don’t use for-in … Read more