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 › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
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, ...
🌐
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.
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
Loop through JavaScript object properties excluding the first two
I’m working with a JavaScript object and I need to iterate over its properties, but I want to skip the first two entries. What’s an efficient way to achieve this? For example, given an object: const data = { a: 1, b: 2, c: 3, d: 4 }; I want to start looping from c onwards. More on community.latenode.com
🌐 community.latenode.com
0
0
October 19, 2024
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 Iterate Through a JavaScript Object? - TestMu AI Community
How can I loop through or enumerate a JavaScript object? I have a JavaScript object like this: var p = { “p1”: “value1”, “p2”: “value2”, “p3”: “value3” }; How do I iterate over this object to access all its elements (p1, p2, p3, etc.) and retrieve their keys and values? More on community.testmuai.com
🌐 community.testmuai.com
0
October 28, 2024
Top answer
1 of 16
5192

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 16
1383

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.

🌐
W3Schools
w3schools.com › js › js_loop_forin.asp
JavaScript For In
The for...in loop is primarily used for objects to access their property names (keys). for (key in object) { // code block to be executed } key A variable that holds the name (key) of each property during the iterations · object The object whose properties are being iterated over · A JavaScript for in statement loops through the properties of a person 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;
    };
  };
};
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-over-a-javascript-object
Iterate over a JavaScript object - GeeksforGeeks
object.keys() Method returns an array of keys of the object and forEach() method is an array method that allows you to iterate over each element in the array.
Published   October 14, 2025
🌐
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.
Find elsewhere
🌐
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 - ... The Object.keys() method was introduced in ES6. It takes the object we want to loop over as an argument and returns an array containing all property names (also known as keys).
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects keys and values in Javascript?
For simultaneous access to keys and values, Object.entries() returns an array of [key, value] pairs, which can be easily looped over with methods such as forEach or for...of. Each of these methods ensures that you can effectively navigate the structure of an object, facilitating data handling and manipulation. const object = { a: 1, b: 2, c: 3 }; for (const [key, value] of Object.entries(object)) { console.log(`Key: ${key}, Value: ${value}`); } An object in JavaScript is a collection of key-value pairs, where each key (also known as a property) of a JavaScript object is associated with a value.
🌐
Flexiple
flexiple.com › javascript › loop-through-an-object
Loop Through an Object In JavaScript – How to Iterate Over an Object in JS - Flexiple
May 15, 2024 - To loop through an object in JavaScript using a for...in loop, you need to access each property and its value. A for...in loop iterates over the enumerable properties of an object.
🌐
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 - Just like with a for and for...of loop, you can use the continue and break operators in a for...in loop. for (let key in lunch) { if (key === 'drink') break; console.log(lunch[key]); } Here’s another demo. The Object.keys() method returns an array of keys from an object.
🌐
DEV Community
dev.to › fpaghar › object-iteration-ways-in-javascript-pej
Object Iteration ways in JavaScript - DEV Community
April 8, 2024 - Object.getOwnPropertyNames() is a method in JavaScript used to iterate over the properties of an object. Unlike methods like Object.keys() or for...in loops, which only iterate over enumerable properties, Object.getOwnPropertyNames() returns ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-iterate-over-a-javascript-object
How to iterate over a JavaScript object ? | GeeksforGeeks
The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-symbol iterable properties of an object. Some objects may contain properties that may be inherited from their prototypes.
Published   August 21, 2024
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
The for...in statement iterates a specified variable over all the enumerable properties of an object. For each distinct property, JavaScript executes the specified statements.
🌐
Latenode
community.latenode.com › other questions › javascript
Loop through JavaScript object properties excluding the first two - JavaScript - Latenode Official Community
October 19, 2024 - I’m working with a JavaScript object and I need to iterate over its properties, but I want to skip the first two entries. What’s an efficient way to achieve this? For example, given an object: const data = { a: 1, b: 2, …
🌐
Hostman
hostman.com › tutorials › looping through objects’s keys and values in javascript
How to loop through objects keys and values in Javascript? | Hostman
August 25, 2025 - The built-in Object.keys() method in JavaScript allows you to get an array of all the keys of a given object. The JS feature is designed to loop over all the properties of an object because it is a simple way to access and manipulate each key individually...
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to Iterate Through a JavaScript Object? - TestMu AI Community
October 28, 2024 - How can I loop through or enumerate a JavaScript object? I have a JavaScript object like this: var p = { “p1”: “value1”, “p2”: “value2”, “p3”: “value3” }; How do I iterate over this object to access all its elements (p1…
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Iterate Over Object Properties in JavaScript | Envato Tuts+
January 19, 2022 - Using a for loop, iterate over the properties of an object with Object.values(). Because the property keys are going to be required in many cases, this is probably not the method you'd want to use when iterating over an object.