Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active June 11, 2021 15:04
Show Gist options
  • Select an option

  • Save MauricioRobayo/84372051bfa3e637fd02e594cda1b818 to your computer and use it in GitHub Desktop.

Select an option

Save MauricioRobayo/84372051bfa3e637fd02e594cda1b818 to your computer and use it in GitHub Desktop.
Iterating objects with for (... in ...) is error prone. It will include enumerable properties from the prototype chain. Do not use unfiltered for (... in ...) statements. Either filter values explicitly with an if statement, or use for (... of Object.keys(...)).
for (const x in someObj) {
if (!someObj.hasOwnProperty(x)) continue;
// now x was definitely defined on someObj
}
for (const x of Object.keys(someObj)) { // note: for _of_!
// now x was definitely defined on someObj
}
for (const [key, value] of Object.entries(someObj)) { // note: for _of_!
// now key was definitely defined on someObj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment