DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Object.keys vs for...in vs Object.entries: When to Use Each
XLinkedInReddit
MediumFrontend Engineering

Object.keys vs for...in vs Object.entries: When to Use Each

Complete comparison of object iteration methods — keys, values, entries, for...in, and when to use each one.

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • Object.keys()
  • for...in
  • Object.entries()
  • Object.values()
  • Comparison Table
  • Performance
  • Best Practices

Iterating over objects is fundamental. Each method has different behavior with inheritance, enumerability, and performance.

Object.keys()

const user = { name: "Rahul", age: 30, role: "engineer" };
Object.keys(user); // ["name", "age", "role"]

// Only own enumerable string-keyed properties
// Does NOT include inherited properties
// Does NOT include Symbol properties

for...in

const parent = { inherited: true };
const child = Object.create(parent);
child.own = true;

for (const key in child) {
  console.log(key); // "own", "inherited" — includes inherited!
}

// To filter own properties:
for (const key in child) {
  if (child.hasOwnProperty(key)) {
    console.log(key); // "own" only
  }
}

Object.entries()

const user = { name: "Rahul", age: 30 };
Object.entries(user); // [["name", "Rahul"], ["age", 30]]

// Perfect for destructuring
for (const [key, value] of Object.entries(user)) {
  console.log(`${key}: ${value}`);
}

// Convert to Map
const map = new Map(Object.entries(user));

Object.values()

const scores = { math: 95, science: 88, english: 92 };
const average = Object.values(scores).reduce((a, b) => a + b, 0) / Object.values(scores).length;

Comparison Table

MethodOwn PropsInheritedSymbolsReturns
Object.keys()✅❌❌string[]
Object.values()✅❌❌any[]
Object.entries()✅❌❌[string, any][]
for...in✅✅❌iterates keys
Object.getOwnPropertyNames()✅❌❌includes non-enumerable
Reflect.ownKeys()✅❌✅everything own

Performance

For large objects (10K+ properties), for...in with hasOwnProperty is slightly faster than Object.keys().forEach() because it doesn't create an intermediate array. But for typical objects, the difference is negligible — choose readability.

Best Practices

  • Default to Object.entries() when you need both key and value
  • Use Object.keys() when you only need keys
  • Avoid for...in unless you specifically need inherited properties
  • Use Reflect.ownKeys() when you need Symbol properties too

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Object.keys()
  • for...in
  • Object.entries()
  • Object.values()
  • Comparison Table
  • Performance
  • Best Practices

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.