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
🌐
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 ...
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
JS Examples JS HTML DOM JS HTML ... JS Interview Prep JS Bootcamp JS Certificate JS Reference ... For Loops can execute a block of code a number of times....
Discussions

How do I loop through or enumerate a JavaScript object? - Stack Overflow
In javascript, every object has a bunch of built-in key-value pairs that have meta-information. When you loop through all the key-value pairs for an object you're looping through them too. hasOwnPropery() filters these out. More on stackoverflow.com
🌐 stackoverflow.com
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
Seeking help with JavaScript Loops

Generally, when you have a loop, you want to structure it as follows.

for( i = (your starting value, which is usually zero); i < (the number of times you want the loop to run, in this case 10); i++ (or however you want to increment the counter);) {}.

So your loop is saying that i = h, but you don't define h. Then, you're saying that you want the loop to run if i == h times, which will result in true or false, which doesn't make sense either.

If I wanted to add F onto the end of a string five times, I would do the following:

function addF(string) {
var result = string;
for (i = 0; i < 5; i++) {
result += "f";
}
return result;
}

Hope that helps!

More on reddit.com
🌐 r/learnjavascript
9
3
September 14, 2017
🌐
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_looping.asp
JavaScript Loops
The for...in loop iterates over the enumerable properties of an object. It is typically used for iterating over object keys. for (key in object) { // code block to be executed } A JavaScript for...in statement loops through the properties of ...
🌐
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, ...
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.

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
For an object car with properties make and model, result would be: ... Although it may be tempting to use this as a way to iterate over Array elements, the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Therefore, it is better to use a traditional for loop with a numeric index when iterating over arrays, because the for...in statement iterates over user-defined properties in addition to the array elements, if you modify the Array object (such as adding custom properties or methods).
Find elsewhere
🌐
W3Schools
w3schools.in › javascript › loops
Understanding JavaScript Loops - W3Schools
The for...in loop is designed for iterating over the properties of an object.
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects keys and values in Javascript?
Learn to navigate Javascript objects efficiently using Object.keys(), Object.values(), and Object.entries() methods to access and manipulate data.
🌐
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.
🌐
W3Schools
w3schools.com › jsref › › jsref_forin.asp
JavaScript for...in Loop
The for...in statements combo iterates (loops) over the properties of an object.
🌐
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); }
🌐
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 - For today’s article, let’s use an object with the details of a lunch order as an example. let lunch = { sandwich: 'turkey', chips: 'cape cod', drink: 'soda' }; We’ll loop over it and log each item to the console, but in a real site or application, you might want to manipulate the data in some way. A for...in loop is similar to the for...of loop we looked at yesterday, but used to loop through objects.
🌐
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
🌐
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 | Nerd For Tech | 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.
🌐
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;
    };
  };
};
🌐
Favtutor
favtutor.com › articles › loop-through-object-javascript
Loop through an Object in JavaScript (4 Methods)
December 16, 2023 - The simplest way to loop through an object in JavaScript is by using the for…in loop.
🌐
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 - Learn how to effectively loop through objects in JavaScript. This guide covers methods like `for...in` and `Object.keys()` with practical examples and tips.