You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

For-of with Object.keys() alternative:

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Notice the use of for-of instead of for-in, if not used it will return undefined on named properties, and Object.keys() ensures the use of only the object's own properties without the whole prototype-chain properties

Using the new Object.entries() method:

Note: This method is not supported natively by Internet Explorer. You may consider using a Polyfill for older browsers.

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (const [key, value] of Object.entries(p)) {
  console.log(`{value}`);
}
Answer from levik on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ for...in
for...in - JavaScript | MDN - MDN Web Docs
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
๐ŸŒ
Mimo
mimo.org โ€บ tutorials โ€บ javascript โ€บ how-to-loop-through-an-object-in-javascript
How to Loop Through an Object in JavaScript
Learn how to loop through an object in JavaScript using Object.entries(), Object.keys(), and Object.values() for keys, values, and key-value pairs.
Discussions

How to loop through a plain JavaScript object with the objects as members - Stack Overflow
How can I loop through all members in a JavaScript object, including values that are objects? For example, how could I loop through this (accessing the "your_name" and "your_message&... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to loop through an object?
You are using the for-in loop wrong: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in You need to declare the new variable. Like this: for (const s in sound) { // . . . } You can also use let/var. Word to the wise though, for...in loops are basically deprecated. It travels up the prototype chain which can lead to unexpected behavior, and that is why you have to have that hasOwnProperty if-block. A better approach would be to combine the newer for...of loop with one of the static Object methods that convert an object into an array: Object.keys Object.values Object.entries That might look like this: function mute() { for (const soundObj of Object.values(sound)) { soundObj.mute = !soundObj.mute; } } Also, worth noting that your function is toggling the mute state of each of these objects. Not sure if that is what you want based on your function name. If you just want to mute each object, you should set .mute to true. More on reddit.com
๐ŸŒ r/learnjavascript
5
2
November 3, 2022
loops - How to iterate over a JavaScript object? - Stack Overflow
When you iterate over an object there is no guarantee to the order in which they will appear. ... Does this answer your question? How to loop through a plain JavaScript object with the objects as members? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Loop through array and create object of specific key/value pairs?
slots[slots.length]={"name":"Name1","task":"Task1"}; That's the most unique way I've seen someone .push() onto the end of an array ๐Ÿ‘€. That's a first for me. As for your problem at hand, here's a more imperative way you could approach it. Create an empty "result" object. We're going to gradually add stuff to it until we get what we want. Loop through each slot in the slots array For each slot, check if it has one of the properties you care about. If it does, add that property to your result object. More on reddit.com
๐ŸŒ r/learnjavascript
6
1
March 31, 2023
Top answer
1 of 12
5196

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

For-of with Object.keys() alternative:

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Notice the use of for-of instead of for-in, if not used it will return undefined on named properties, and Object.keys() ensures the use of only the object's own properties without the whole prototype-chain properties

Using the new Object.entries() method:

Note: This method is not supported natively by Internet Explorer. You may consider using a Polyfill for older browsers.

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (const [key, value] of Object.entries(p)) {
  console.log(`{value}`);
}
2 of 12
1385

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

You can combine for...of, destructuring, and Object.entries:

for (const [key, value] of Object.entries(obj)) {
    console.log(key, value);
}

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-iterate-over-objects-in-javascript
Loop Through an Object in JavaScript โ€“ How to Iterate Over an Object in JS
November 7, 2024 - In this tutorial, you learned that the best way to loop through an object is to use any object static method based on your needs to first convert to an array before looping.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Object โ€บ entries
Object.entries() - JavaScript - MDN Web Docs
4 days ago - Using array destructuring, you can iterate through objects easily. ... // Using for...of loop const obj = { a: 5, b: 7, c: 9 }; for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } // Using array methods Object.entries(obj).forEach(([key, ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-iterate-over-a-javascript-object
Iterate over a JavaScript object - GeeksforGeeks
In JavaScript, object iteration involves accessing its properties one by one to read, modify, or process their keys and values. for...in iterates over enumerable properties, including inherited ones.
Published: August 19, 2026
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ loop-through-object
JavaScript Program to Loop Through an Object
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to loop through an object using for...in loop const student = { name: 'John', age: 20, hobbies: ['reading', 'games', 'coding'], }; // using for...in for (let key in student) { let value; // get the value value = student[key]; console.log(key + " - " + value); }
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_object_iterations.asp
JavaScript Object Iterations
The Object.assign() method copies properties from one or more source objects to a target object.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ how to loop through an object?
r/learnjavascript on Reddit: How to loop through an object?
November 3, 2022 -

hello,

i want to loop through an object (sound) which contains objects (s) which contain boolean properties (mute).

For that i use a for in loop. But motherfucker says "s" is not defined. But s IS defined. What is wrong with my code?

function mute(){
  for(s in sound){
    if(sound.hasOwnPropertyKey(s)){
    sound[s].mute = !sound[s].mute;
    };
  };
};
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ javascript-object-iteration-methods
10 Effective Methods to Iterate Over JavaScript Objects
We will look at ten distinct JavaScript methods for iterating over an object in this article. For every technique, we'll offer special instances and go over when to use them. One of the easiest methods for iterating over an object's attributes is to use a ยท for...in loop.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-loop-and-enumerate-javascript-object-properties
How to Loop and Enumerate JavaScript Object Properties
February 9, 2022 - In this guide, we will take a look at how to loop and enumerate objects in JavaScript - from the simpler approaches, such as using a for...in loop to the new object static methods that were introduced in ES6 and ES8. Prior to the introduction of ES6, the only way to loop through objects in JavaScript was to obtain key-value through the use of a for...in loop.
๐ŸŒ
Rune Hub
rune.codes โ€บ home โ€บ tutorials โ€บ javascript โ€บ looping through objects in javascript: complete guide
Loop Through Objects in JavaScript: Complete Guide | Rune Hub
July 14, 2026 - But JavaScript gives you several built-in methods that turn an object into something you can loop over. The three main tools are Object.keys(), Object.values(), and Object.entries().
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ looping-through-objects-s-keys-and-values-in-javascript
How to loop through objects keys and values in Javascript? | Hostman
Also they contain any number of key-value pairs with different data types. The loop through object feature in JS stands for creating complex objects, suitable for storing, sorting, filtering, searching and manipulating large amounts of data.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ loop through an object
JavaScript Program to Loop Through an Object | Vultr Docs
December 17, 2024 - Since objects play a crucial role in representing real-world data within applications, understanding how to efficiently traverse them is essential for tasks like data processing, transformation, and display. In this article, you will learn how to loop through properties of a JavaScript object using various methods, such as for...in, Object.keys(), and Object.entries().
๐ŸŒ
Latenode
latenode.com โ€บ home โ€บ blog โ€บ how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
September 3, 2026 - This ensures that you only iterate over the object's own properties and avoid any inherited properties. The for...in javascript loop through object is a straightforward way to iterate over an object's properties.
๐ŸŒ
Altcademy
altcademy.com โ€บ blog โ€บ how-to-loop-through-object-in-javascript
How to loop through object in JavaScript - Altcademy.com
June 9, 2023 - Looping through an object means iterating over its properties one by one, usually to perform some operation on the property values or keys. There are several ways to do this in JavaScript, and we'll explore them one by one.
Top answer
1 of 16
1344

For iterating on keys of Arrays, Strings, or Objects, use for .. in :

for (let key in yourobject) {
  console.log(key, yourobject[key]);
}

With ES6, if you need both keys and values simultaneously, do

for (let [key, value] of Object.entries(yourobject)) {
    console.log(key, value);
}

To avoid logging inherited properties, check with hasOwnProperty :

for (let key in yourobject) {
   if (yourobject.hasOwnProperty(key)) {
      console.log(key, yourobject[key]);
   }
}

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 and their properties.

If you want to do it "in chunks", the best is to extract the keys in an array. As the order isn't guaranteed, this is the proper way. In modern browsers, you can use

let keys = Object.keys(yourobject);

To be more compatible, you'd better do this :

 let keys = [];
 for (let key in yourobject) {      
     if (yourobject.hasOwnProperty(key)) keys.push(key);
 }

Then you can iterate on your properties by index: yourobject[keys[i]] :

for (let i=300; i < keys.length && i < 600; i++) { 
   console.log(keys[i], yourobject[keys[i]]);
}
2 of 16
101

Here is another iteration solution for modern browsers:

Object.keys(obj)
  .filter((k, i) => i >= 100 && i < 300)
  .forEach(k => console.log(obj[k]));

Or without the filter function:

Object.keys(obj).forEach((k, i) => {
    if (i >= 100 && i < 300) {
        console.log(obj[k]);
    }
});

However you must consider that properties in JavaScript object are not sorted, i.e. have no order.

๐ŸŒ
Medium
medium.com โ€บ nerd-for-tech โ€บ how-to-loop-through-an-object-in-javascript-e1625a5bc3e7
How To Loop Through An Object in JavaScript | by Simon Ugorji | Medium
June 16, 2022 - In this approach, we will use a for-each loop to loop through the object's keys which are returned as an Array, then with a particular key as the element at hand, we can access the value assigned to it.
๐ŸŒ
DEV Community
dev.to โ€บ shameel โ€บ looping-through-javascript-objects-with-different-approaches-3c6m
๐Ÿ”„ Looping Through JavaScript Objects with Different Approaches ๐Ÿ”„ - DEV Community
October 27, 2023 - The Object.keys() method returns an array of an object's own enumerable property names. You can then loop through this array using a for...of loop or other iteration techniques.