How do you iterate over object properties?
The short answer
The main ways are Object.keys() (get keys), Object.values() (get values), Object.entries() (get key-value pairs), and for...in (loop over all enumerable properties including inherited ones). Object.keys/values/entries are preferred because they only return the object's own properties.
Object.keys
Returns an array of the object's own property names:
1const user = { name: 'John', age: 30, city: 'NYC' };23Object.keys(user); // ["name", "age", "city"]45Object.keys(user).forEach((key) => {6 console.log(`${key}: ${user[key]}`);7});
Object.values
Returns an array of the values:
1Object.values(user); // ["John", 30, "NYC"]
Object.entries
Returns an array of [key, value] pairs — the most useful for iteration:
1Object.entries(user); // [["name", "John"], ["age", 30], ["city", "NYC"]]23for (const [key, value] of Object.entries(user)) {4 console.log(`${key}: ${value}`);5}
for...in
Loops over all enumerable properties, including inherited ones:
1for (const key in user) {2 console.log(`${key}: ${user[key]}`);3}
If you use for...in, always check hasOwnProperty to skip inherited properties:
1for (const key in user) {2 if (user.hasOwnProperty(key)) {3 console.log(`${key}: ${user[key]}`);4 }5}
Or just use Object.keys() / Object.entries() which do not include inherited properties.
Interview Tip
Show Object.entries() with destructuring as the cleanest approach. Mention that for...in includes inherited properties (which is why Object.keys is preferred). This is a quick question — keep examples short.
Why interviewers ask this
This tests basic JavaScript knowledge. Interviewers want to see if you know the different methods and which ones include inherited properties.
Related JavaScript interview topics
Continue with nearby questions from the same topic cluster.