In this article I want to talk about three different ways to traverse an object’s properties in JavaScript and the varying results you can expect when using them. Namely:

  • for ... in loops
  • Object.keys()
  • Object.getOwnPropertyNames()

And we’re going to look at the differences between them using goats.

Image for post
Image for post
Unexpected JavaScript assistant. Photo by Lamna The Shark on Unsplash

So first of all, let’s create a myGoat object, with a custom goat prototype using Object.create, and then let’s look at the different ways we can loop over its properties.

Before we get into looping through myGoat’s properties, let’s talk a little about object properties in general. All object properties have attributes


Regardless of the language you use to write code, your program needs to allocate and access memory in order to store variables. In many high-level languages memory management is carried out for you, which makes for a simpler coding experience. Even if you’re using a high-level language it is still a good idea to have an understanding of the memory management process, and its potential pitfalls.

So, what do we mean by memory management? Low-level languages like C include memory management commands such as malloc( ) and free( ) which allow developers to explicitly manage memory allocation and deallocation. No…

Alexandra Langton

Alexandra lives in Brooklyn, where she spends most of her time writing code with her cat.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store