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
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.

🌐
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, ...
Discussions

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
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
What's the best way to iterate over JavaScript object properties?
I need to go through each property in this object and access both the property names (name, age, city) and their corresponding values. What are the different methods I can use to iterate through all the properties of this object? I want to be able to process each key-value pair inside the loop. More on community.latenode.com
🌐 community.latenode.com
0
0
August 21, 2025
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
🌐
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.
🌐
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.
🌐
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); }
🌐
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;
    };
  };
};
Find elsewhere
🌐
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
🌐
YouTube
youtube.com › watch
4 Easy Ways to Loop Through Objects in JavaScript - YouTube
Here are 4 easy methods you can use for looping through the properties of an object in JavaScript00:00 List the approaches00:33 for...in statement01:23 Objec...
Published: August 19, 2022
🌐
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.
🌐
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.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-object
Iterating Through an Object with `forEach()` - Mastering JS
But you can iterate over a JavaScript object using forEach() if you transform the object into an array first, using Object.keys(), Object.values(), or Object.entries().
🌐
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.
🌐
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.
🌐
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.
🌐
Go Make Things
gomakethings.com › whats-the-best-way-to-loop-over-an-object-with-javascript
What's the best way to loop over an object with JavaScript? | Go Make Things
January 11, 2022 - // logs [["sandwich", "turkey"], ["chips", "cape cod"], ["drink", "soda"]] let entries = Object.entries(lunch); console.log(entries); You can combine this technique with a for...of loop and array destructuring to loop through the object.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › loop through an object
JavaScript Program to Loop Through an Object
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().