How to Iterate Over an Object in JavaScript: 5 Ways with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Iterate Over an Object in JavaScript: From Beginner to Pro

\n\n

Every developer sooner or later faces the task: "How do I go through all the properties of an object?" The question is so popular that on Stack Overflow it has gathered thousands of views and hundreds of answers. And it's no surprise — objects in JavaScript are the foundation of everything: from configurations to data from the server.

\n\n

In this tutorial, we will cover all the main ways to iterate over an object in JavaScript, from classics to modern methods. You will learn when to use for...in, how Object.keys is better, and how Object.entries simplifies life. Let's go!

\n\n

Preparation: Creating a Test Object

\n\n

To make the examples clear, let's take a simple user object:

\n\n
const user = {\n  name: 'Anna',\n  age: 28,\n  city: 'Moscow',\n  isAdmin: false\n};
\n\n

Our task is to output all keys and values. Let's see what tools are in the JavaScript arsenal.

\n\n

1. The for...in Loop — A Classic

\n\n

The oldest and most basic way to iterate over object properties. It appeared back in ES3 and works in all browsers.

\n\n
for (let key in user) {\n  console.log(key + ': ' + user[key]);\n}\n// name: Anna\n// age: 28\n// city: Moscow\n// isAdmin: false
\n\n

Important nuance: for...in iterates not only over own properties but also over inherited ones through the prototype. To filter them out, use hasOwnProperty:

\n\n
for (let key in user) {\n  if (user.hasOwnProperty(key)) {\n    console.log(key + ': ' + user[key]);\n  }\n}
\n\n

Modern objects rarely have extra prototype properties, but the habit of checking hasOwnProperty will protect against bugs when working with libraries.

\n\n

2. Object.keys() — A Modern Approach

\n\n

The Object.keys() method returns an array of an object's own keys. This eliminates problems with prototypes and gives access to all array methods.

\n\n
const keys = Object.keys(user);\nconsole.log(keys); // ['name', 'age', 'city', 'isAdmin']\n\nkeys.forEach(key => {\n  console.log(key + ': ' + user[key]);\n});
\n\n

Advantages:

\n
    \n
  • Works only with own properties (not inherited)
  • \n
  • Returns an array — you can use forEach, map, filter
  • \n
  • Readable and predictable code
  • \n
\n\n

This is my favorite way when you just need to get a list of keys and work with them.

\n\n

3. Object.values() — When You Only Need Values

\n\n

Sometimes we are only interested in the values of properties, without the keys. For example, for summing or displaying data.

\n\n
const values = Object.values(user);\nconsole.log(values); // ['Anna', 28, 'Moscow', false]\n\nvalues.forEach(value => {\n  console.log(value);\n});
\n\n

The method appeared in ES2017 and is great for working with data collections where keys don't matter.

\n\n

4. Object.entries() — The King of Methods

\n\n

Object.entries() returns an array of arrays, where each nested array is a [key, value] pair. This is a universal tool for iterating over an object in JavaScript.

\n\n
const entries = Object.entries(user);\nconsole.log(entries);\n// [\n//   ['name', 'Anna'],\n//   ['age', 28],\n//   ['city', 'Moscow'],\n//   ['isAdmin', false]\n// ]\n\nentries.forEach(([key, value]) => {\n  console.log(`${key}: ${value}`);\n});
\n\n

Notice the destructuring in the forEach parameters: ([key, value]). This makes the code clean and concise.

\n\n

When to use:

\n
    \n
  • You need both keys and values
  • \n
  • You want to transform an object into another structure (e.g., into a Map)
  • \n
  • You are writing a function

Blogs

Book Recommendations