7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. JavaScript

How to Iterate Over Object Keys With JavaScript

Read Time: 2 mins

Custom objects cannot be iterated over using the for...of loop. In addition, you can't use iterator methods like map() and forEach(). If you do, you'll get a TypeError in every instance. 

Instead, use for...in to iterate over objects. This method iterates over all of the object's enumerable, non-symbol properties.

In the following example, we use it to iterate over all three properties of obj, and for each property, we log a string consisting of the property name (i.e. its key) and its corresponding value.

We initialized the loop with a variable, prop, which will hold a different property name (aka key) on each iteration of the object properties. The code specified within the block will run on each iteration.

Here's a little demo to demonstrate that (click on Result to see the output):

Inside the loop, on every iteration, we log one of the object's property names and values to the console.

Another way to iterate over an object's properties is by passing the object inside Object.entries() and calling the method. This will return all the enumerable properties of that object inside a multidimensional array (array of arrays):

Then we can loop through that using any of the array iterator methods:

You can also use a for...of loop:

Here are some demos:

Here, because we get an array consisting of the key and value on each iteration, we simply access them with entry[0] and entry[1] respectively. If you want just the keys instead of the key and value, use Object.keys() in place of Object.entries().

for...of is basically the same:


These are the ways we can easily iterate over an object's properties and keys in JavaScript. Check out our JavaScript posts for more articles and tutorials.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.