The for..of loop only supports iterable objects like arrays, not objects.

To iterate over the values of an object, use:

for (var key in test) {
    var item = test[key];
}
Answer from Overv on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ for...of
for...of - JavaScript | MDN - MDN Web Docs
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. Iterable objects include instances of built-ins such as Array, String, TypedArray, Map, Set, NodeList (and other DOM collections), as well as the arguments object, generators produced ...
Discussions

How do I loop through or enumerate a JavaScript object? - Stack Overflow
Why didn't the standard provide ... own forEach property. I suppose Object.keys guards against the callback modifying the object's keys. 2014-06-23T20:36:11.313Z+00:00 ... Python is so easy, javascript I have to look up basics every time. 2022-08-23T12:32:48.387Z+00:00 ... @DavidHarkness Why make it easy when it can be hard? Programmers LOVE to over-engineer things. 2024-09-30T06:48:46.327Z+00:00 ... But be very careful when using this kind of loop, because this ... 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
How to loop through a plain JavaScript object with the objects as members - Stack Overflow
I think there is two use cases ... the loop process. ... However, at the time of the edit I wouldn't recommend the ES7 method, because JavaScript initializes a lot of variables internally to build this procedure (see the feedbacks for proof). Unless you are not developing a huge application which deserves optimization, then it is OK, but if optimization is your priority, you should think about it. ... const keys = Object.keys(obj); ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
For...in vs Object.keys
It's largely preference or depending on whether or not you're dealing with a more imperative or more functional codebase. But you should at least be aware of the differences that go beyond style. Specifically, for..in will capture inherited keys whereas Object.keys sticks only to own properties. For example: var par = { prop1 : "some val" }; var obj = Object.create(par); obj.prop2 = "some other val"; for(key in obj){ console.log("Key: ", key) console.log("Value: ", obj[key]) } // ^ prop1 and prop2 Object.keys(obj).forEach((key)=>{ console.log("For Each Key: ", key) console.log("For Each Value: ", obj[key]) }) // ^ prop2 only More on reddit.com
๐ŸŒ r/javascript
24
14
April 25, 2018
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_loop_forof.asp
JavaScript For Of
It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more: for (variable of iterable) { // code block to be executed } variable - For every iteration the value of the next property is assigned to the variable.
๐ŸŒ
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 - let arr = [24, 33, 77]; arr.forEach((val) => console.log(val)); // โœ…โœ…โœ… for (val of arr) { console.log(val); // โœ…โœ…โœ… } let obj = { age: 12, name: "John Doe" }; obj.forEach((val) => console.log(val)); // โŒโŒโŒ for (val of obj) { console.log(val); // โŒโŒโŒ } In this article, You'll learn how you can loop through an object in JavaScript.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ for-of
JavaScript for... of Loop
The for..of loop in JavaScript allows you to iterate over iterable objects (arrays, sets, maps, strings etc).
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_loop_for.asp
JavaScript for Loop
The for statement creates a loop with 3 optional expressions: for (exp 1; exp 2; exp 3) { // code block to be executed } exp 1 is executed (one time) before the execution of the code block.
๐ŸŒ
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" });
Find elsewhere
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.

๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ javascript-object-iteration-methods
10 Effective Methods to Iterate Over JavaScript Objects
November 20, 2024 - 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.
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 โ€บ javascript-for-of-loop
JavaScript for...of Loop - GeeksforGeeks
August 5, 2025 - The JavaScript for...of loop is a modern, iteration statement introduced in ECMAScript 2015 (ES6). Works for iterable objects such as arrays, strings, maps, sets, and more.
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ javascript for of loop
Javascript For Of Loop: Definition & Examples | StudySmarter
The JavaScript "for...of" loop is a powerful tool that allows you to iterate over iterable objects like arrays, strings, maps, and sets, extracting values directly without the need for a counter. It's similar to the "forEach" method but provides added flexibility by supporting breaks and continues ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript โ€บ javascript_forof_loop.htm
JavaScript - For...of Loop
The for...of loop in JavaScript is used to traverse elements of the iterable object. In each iteration, it gives an element of the iterable object. Iterable objects include arrays, strings, maps, sets, and generators.
๐ŸŒ
Exploring JS
exploringjs.com โ€บ es6 โ€บ ch_for-of.html
17. The for-of loop
for-of is a new loop in ES6 that replaces both for-in and forEach() and supports the new iteration protocol. Use it to loop over iterable objects (Arrays, strings, Maps, Sets, etc.; see Chap.
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ loop-through-object-javascript
How to loop through objects keys and values in Javascript?
March 10, 2022 - The for...in loop in JavaScript is specifically designed to iterate over the properties of an object, accessing each key and its associated value. This loop syntax allows developers to run a block of code for each enumerable property found in ...
๐ŸŒ
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 ...