Iterating over properties requires this additional hasOwnProperty check:

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

It's necessary because an object's prototype contains additional properties for the object which are technically part of the object. These additional properties are inherited from the base object class, but are still properties of obj.

hasOwnProperty simply checks to see if this is a property specific to this class, and not one inherited from the base class.


It's also possible to call hasOwnProperty through the object itself:

if (obj.hasOwnProperty(prop)) {
    // do stuff
}

But this will fail if the object has an unrelated field with the same name:

var obj = { foo: 42, hasOwnProperty: 'lol' };
obj.hasOwnProperty('foo');  // TypeError: hasOwnProperty is not a function

That's why it's safer to call it through Object.prototype instead:

var obj = { foo: 42, hasOwnProperty: 'lol' };
Object.prototype.hasOwnProperty.call(obj, 'foo');  // true
Answer from user2417527 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 loop below iterates over all of the object's enumerable, non-symbol properties and logs a string of the property names and their values.
Discussions

How do I loop through or enumerate a JavaScript object? - Stack Overflow
Pass your object to Object.keys(). This will return an array containing all the keys in the object. You can then loop through the array using map. Using obj[key] where obj is your object and key is the current value in the map iteration, you can get the value for that key/property. More on stackoverflow.com
🌐 stackoverflow.com
loops - How to iterate over a JavaScript object? - Stack Overflow
I have an object in JavaScript: { abc: '...', bca: '...', zzz: '...', xxx: '...', ccc: '...', // ... } I want to use a for loop to get its properties. And I want to iterate... More on stackoverflow.com
🌐 stackoverflow.com
Iterate over Object Literal Values - javascript
The bit inside the square brackets should be any JS expression that returns a string so if you say defaults[starts] is looking for a variable starts, which you don't have so that returns undefined. Read this: developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects 2012-02-20T03:53:5... More on stackoverflow.com
🌐 stackoverflow.com
looping through dictionary to get to arrays yields the name of the array and not its values?
Using a for...in goes over the keys of an object. So k will be a key, and i will be a key within a key. You'd be better off using a for...of loop. It will give you direct values which it looks like you're expecting. For objects/dictionaries, you'd first need to get what you want to iterate over using Object.values/keys/entries since they're not inherently iterable. for (const array of Object.values(dictionary)) { for (const element of array) { console.log(element) } } More on reddit.com
🌐 r/learnjavascript
6
1
January 9, 2023
Top answer
1 of 16
2606

Iterating over properties requires this additional hasOwnProperty check:

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

It's necessary because an object's prototype contains additional properties for the object which are technically part of the object. These additional properties are inherited from the base object class, but are still properties of obj.

hasOwnProperty simply checks to see if this is a property specific to this class, and not one inherited from the base class.


It's also possible to call hasOwnProperty through the object itself:

if (obj.hasOwnProperty(prop)) {
    // do stuff
}

But this will fail if the object has an unrelated field with the same name:

var obj = { foo: 42, hasOwnProperty: 'lol' };
obj.hasOwnProperty('foo');  // TypeError: hasOwnProperty is not a function

That's why it's safer to call it through Object.prototype instead:

var obj = { foo: 42, hasOwnProperty: 'lol' };
Object.prototype.hasOwnProperty.call(obj, 'foo');  // true
2 of 16
1346

As of JavaScript 1.8.5 you can use Object.keys(obj) to get an Array of properties defined on the object itself (the ones that return true for obj.hasOwnProperty(key)).

Object.keys(obj).forEach(function(key,index) {
    // key: the name of the object key
    // index: the ordinal position of the key within the object 
});

This is better (and more readable) than using a for-in loop.

Its supported on these browsers:

  • Firefox (Gecko): 4 (2.0)
  • Chrome: 5
  • Internet Explorer: 9

See the Mozilla Developer Network Object.keys()'s reference for futher information.

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.

Top answer
1 of 16
1341

For iterating on keys of Arrays, Strings, or Objects, use for .. in :

for (let key in yourobject) {
  console.log(key, yourobject[key]);
}

With ES6, if you need both keys and values simultaneously, do

for (let [key, value] of Object.entries(yourobject)) {
    console.log(key, value);
}

To avoid logging inherited properties, check with hasOwnProperty :

for (let key in yourobject) {
   if (yourobject.hasOwnProperty(key)) {
      console.log(key, yourobject[key]);
   }
}

You don't need to check hasOwnProperty when iterating on keys if you're using a simple object (for example one you made yourself with {}).

This MDN documentation explains more generally how to deal with objects and their properties.

If you want to do it "in chunks", the best is to extract the keys in an array. As the order isn't guaranteed, this is the proper way. In modern browsers, you can use

let keys = Object.keys(yourobject);

To be more compatible, you'd better do this :

 let keys = [];
 for (let key in yourobject) {      
     if (yourobject.hasOwnProperty(key)) keys.push(key);
 }

Then you can iterate on your properties by index: yourobject[keys[i]] :

for (let i=300; i < keys.length && i < 600; i++) { 
   console.log(keys[i], yourobject[keys[i]]);
}
2 of 16
101

Here is another iteration solution for modern browsers:

Object.keys(obj)
  .filter((k, i) => i >= 100 && i < 300)
  .forEach(k => console.log(obj[k]));

Or without the filter function:

Object.keys(obj).forEach((k, i) => {
    if (i >= 100 && i < 300) {
        console.log(obj[k]);
    }
});

However you must consider that properties in JavaScript object are not sorted, i.e. have no order.

🌐
Index.dev
index.dev › blog › javascript-object-iteration-methods
10 Effective Methods to Iterate Over JavaScript Objects
If you're working with a single object, you can't use this directly. ... for...of loop. You must first transform the item into an iterable structure, such as an array, in order to utilize it with it. const movie = { title: 'Inception', director: 'Christopher Nolan', year: 2010 }; for (const [key, value] of Object.entries(movie)) { console.log(`${key}: ${value}`); }
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects keys and values in Javascript?
Object.values() gives an array of the object's own enumerable property values. Object.entries() returns an array of an object's enumerable properties in [key, value] pairs. Utilizing these methods simplifies iterating over objects, enabling ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › values
Object.values() - JavaScript - MDN Web Docs
An array containing the given object's own enumerable string-keyed property values. Object.values() returns an array whose elements are values of enumerable string-keyed properties found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates ...
🌐
Esdiscuss
esdiscuss.org › topic › es6-iteration-over-object-values
ES6 iteration over object values
September 28, 2014 - On 3/14/2014 5:16 PM, Mark Volkmann wrote: > Does ES6 add any new ways to iterate over the values in an object? > I've done a lot of searching, but haven't seen anything. > I'm wondering if there is something more elegant than this: > > Object.keys(myObj).forEach(function (key) { > let obj = myObj[key]; > // do something with obj > }); Not built in, but ES6 does provide a better story for this using generators and for-of: ```js // using a generator function function* entries(obj) { for (let key of Object.keys(obj)) { yield [key, obj[key]]; } } // an alternative version using a generator expression function entries(obj) { return (for (key of Object.keys(obj)) [key, obj[key]]); } for (let [key, value] of entries(myObj)) { // do something with key|value } ``` # David Bruant (12 years ago) History ·
🌐
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 - To use a similar for loop to iterate through object keys and values, you should apply the for...in loop designed specifically for objects: for (var key in object) { // code to be executed } In this loop, the variable key takes on the value of ...
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Attacomsian
attacomsian.com › blog › javascript-iterate-objects
How to iterate over object keys and values in JavaScript
November 12, 2022 - The for...in loop is used for iterating over keys of objects, arrays, and strings. The Object.keys() method returns an array of object keys. The Object.values() method returns the values of all properties in the object as an array.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Iterate Over Object Properties in JavaScript | Envato Tuts+
January 19, 2022 - To further simplify the code, we ... is much cleaner and more readable. Object.entries() is the recommended method for iterating over an object's properties in JavaScript....
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-object
Iterating Through an Object with `forEach()` - Mastering JS
May 29, 2020 - You can then iterate over each key in the object using forEach(). const obj = { name: 'Jean-Luc Picard', rank: 'Captain' }; // Prints "name Jean-Luc Picard" followed by "rank Captain" Object.keys(obj).forEach(key => { console.log(key, obj[key]); ...
🌐
Hexlet
hexlet.io › courses › js-objects › lessons › for-of › theory_unit
Iterating over object properties | JS: Objects
If you don't need keys, you can get an array of the object's property values immediately with the Object.values(obj)method: const course = { name: 'JS: React', slug: 'js-react' }; const values = Object.values(course); // [ 'JS: React', 'js-react' ] for (const value of values) { console.log(value); }Expand code
🌐
Latenode
latenode.com › home › blog › development & programming › javascript for automation › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
February 12, 2026 - The Object.entries() method provides a convenient way to get an array of key-value pairs from an object. By combining it with the map() method, you can easily iterate over the array and perform operations on each key-value pair.
🌐
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.
🌐
DEV Community
dev.to › fpaghar › object-iteration-ways-in-javascript-pej
Object Iteration ways in JavaScript - DEV Community
April 8, 2024 - It's a convenient way to iterate over object properties when you're only interested in keys. const obj = { a: 1, b: 2, c: 3 }; Object.keys(obj).forEach(key => { console.log(key + ': ' + obj[key]); }); Remember that Object.keys() ignores inherited ...
🌐
Educative
educative.io › answers › how-to-iterate-through-the-objects-properties-in-javascript
How to iterate through the object's properties in JavaScript
for (const [key, value] in Object.entries(objectName)) { ... Line 2-5: We create an object named data. Line 8: We use the Object.entries() method to iterate over the properties of data.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-over-a-javascript-object
Iterate over a JavaScript object - GeeksforGeeks
Use map() to access each pair, where index 0 is the key and index 1 is the value. ... function iterateObject() { let exampleObj = { book: "Sherlock Holmes", author: "Arthur Conan Doyle", genre: "Mystery" }; Object.entries(exampleObj).map(entry ...
Published   October 14, 2025