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, value]) => { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" });
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.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...in
for...in - JavaScript | MDN - MDN Web Docs
You can use a block statement to execute multiple statements. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its prototype chain (properties of nearer prototypes take precedence over those of prototypes further away from the object ...
🌐
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 ...
🌐
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;
    };
  };
};
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects keys and values in Javascript?
Utilize methods that allow for efficient enumeration and access to iterate through an object's keys and values in JavaScript. The for...in loop offers a simple way to traverse all enumerable properties of an object, enabling you to access each ...
🌐
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.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
The for...of statement creates a loop Iterating over iterable objects (including Array, Map, Set, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property.
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 - Before ES6, we relied on the for...in method whenever we wanted to loop through an object. The for...in loop iterates through properties in the prototype chain.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-over-a-javascript-object
Iterate over a JavaScript object - GeeksforGeeks
A for..in loop iterates over all enumerable properties of an object, including inherited ones.
Published   October 14, 2025
🌐
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.
🌐
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 - Discover efficient techniques for iterating through object keys and values in JavaScript. Master dynamic data manipulation in your applications.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Latenode
community.latenode.com › other questions › javascript
Loop through JavaScript object properties excluding the first two - JavaScript - Latenode Official Community
October 19, 2024 - 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. How can I do this? You might refer to the concept of object traversal on the Wikipedia page on JavaScript syntax.
🌐
Programiz
programiz.com › javascript › examples › loop-through-object
JavaScript Program to Loop Through an Object
The Object.entries() method returns an array of a given object's key/value pairs. The for...of loop is used to loop through an array.
🌐
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 - This one is pretty straightforward for me: Object.entries() with for...of. Having a dedicated variable for the item in the loop is really nice.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › loop-through-an-object
JavaScript Program to Loop Through an Object | Vultr Docs
December 17, 2024 - It provides an easy way to access each key and therefore can be used to manipulate or review object data. Define an object with multiple properties. Use a for...in loop to iterate over the object.
🌐
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
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
JS Examples JS HTML DOM JS HTML ... Interview Prep JS Bootcamp JS Certificate JS Reference ... For Loops can execute a block of code a number of times....