01 / 21

Iterating
Objects

Learn how to loop through keys, values, and entries of JavaScript objects using built-in methods like Object.keys() and Object.entries().

JavaScript

What is a JavaScript Object?

An object is a collection of key-value pairs. Each key (also called a property name) maps to a value. Think of it like a dictionary: each word (key) has a definition (value).

const student = { name: "Alice", age: 16, grade: "11th" };

Why Iterate Over Objects?

When you need to process every property in an object (display user settings, calculate totals, transform data), you need a way to loop through all key-value pairs systematically.

Display
Render every field in a user profile to the screen
Transform
Convert all prices from dollars to euros
Validate
Check that no field in a form is empty

The Big Three Methods

JavaScript gives you three static methods on the Object constructor to extract data from any object:

Object.keys()
Returns an array of keys (property names)
Object.values()
Returns an array of values
Object.entries()
Returns an array of [key, value] pairs

Object.keys() Visualized

student name "Alice" age 16 grade "11th" Object.keys() Result Array [0] "name" [1] "age" [2] "grade"

Object.values() Explained

Object.values() does the opposite of Object.keys(): it extracts only the values from each property, returning them as an array.

const student = { name: "Alice", age: 16, grade: "11th" }; const vals = Object.values(student); console.log(vals);
Output ["Alice", 16, "11th"]

Object.entries() Explained

Object.entries() returns an array of [key, value] pairs. Each pair is itself a two-element array, giving you both pieces of information at once.

const student = { name: "Alice", age: 16 }; const pairs = Object.entries(student); console.log(pairs);
Output [ ["name", "Alice"], ["age", 16] ]

for...in vs Object Methods

JavaScript also has a for...in loop for iterating objects. But it behaves differently from Object.keys/values/entries in an important way.

for...in Object.keys/values/entries
Inherited properties ✕ Includes them ✓ Own properties only
Returns Keys one at a time A full array
Array methods ✕ Not chainable ✓ .map(), .filter(), etc.
Recommended Rarely ✓ Preferred approach

How to Iterate Step by Step

1
Choose your method. Need keys only? Use Object.keys(). Need both? Use Object.entries().
2
Pass the object. Call the method with your object as the argument: Object.keys(myObj).
3
Loop the result. Use .forEach(), for...of, or .map() on the returned array.
4
Destructure (optional). With entries, use [key, value] destructuring for clean code.

Example 1: Object.keys() + forEach

Print every key from a scores object:

const scores = { math: 95, english: 88, science: 92 }; Object.keys(scores).forEach(key => { console.log(key, scores[key]); });
Output math 95
english 88
science 92

Example 2: Object.entries() + Destructuring

Build a formatted string of all product details:

const product = { name: "Laptop", price: 999, inStock: true }; for (const [key, value] of Object.entries(product)) { console.log(`${key}: ${value}`); }
Output name: Laptop
price: 999
inStock: true

Example 3: Transforming with .map()

Create an array of formatted labels from an object:

const colors = { sky: "blue", grass: "green", sun: "yellow" }; const labels = Object.entries(colors) .map(([thing, color]) => `The ${thing} is ${color}`); console.log(labels);
Output ["The sky is blue", "The grass is green", "The sun is yellow"]

Filtering Object Properties

Combine Object.entries() with .filter() to select only the properties you need:

const inventory = { apples: 5, bananas: 0, oranges: 12 }; const inStock = Object.entries(inventory) .filter(([item, qty]) => qty > 0); console.log(inStock);
Output [ ["apples", 5], ["oranges", 12] ]

The Data Flow Pipeline

OBJECT { key: val, key: val, ... } Object.entries() [ [k, v], [k, v], ... ] .forEach / .map Process each pair ✓ Done Source Convert Iterate

Common Mistakes

✕ Using dot notation with variables
Writing obj.key when key is a variable. You need obj[key] (bracket notation) to dynamically access properties.
✕ Calling .keys() on the object directly
Writing myObj.keys() instead of Object.keys(myObj). keys() is a static method on the Object constructor, not on instances.
✕ Forgetting for...in includes inherited
Using for...in without hasOwnProperty check. Inherited prototype properties will sneak into your loop unexpectedly.

Summary Comparison

Method Returns Best For Example Output
Object.keys() Array of strings Listing property names ["name", "age"]
Object.values() Array of values Summing, displaying ["Alice", 16]
Object.entries() Array of [k, v] Transforming, filtering [["name","Alice"], ["age", 16]]
for...in Keys (one at a time) Rarely recommended Includes inherited

Question 1 of 5

What does Object.keys() return?

Question 2 of 5

Given const obj = { a: 1, b: 2 }, what is the result of Object.entries(obj)?

Question 3 of 5

Why is obj[key] used instead of obj.key when iterating with a variable?

Question 4 of 5

What is one key disadvantage of using for...in compared to Object.keys()?

Question 5 of 5

Which code correctly logs every value in const pet = { name: "Rex", type: "dog" }?