Course contentsShow
← JavaScript
Lesson 642 of 2,72315. ObjectsFree lesson

Object.values() and Object.entries()

Object.values() returns values; Object.entries() returns [key, value] pairs, both as arrays.

Object.values() and Object.entries()

You'll learn: How to extract just the values from an object, or get key-value pairs together as arrays.

What Are These Methods?

While Object.keys() gives you property names, sometimes you need the values themselves or both keys and values together. That's where these two methods come in:

  • Object.values(obj) returns an array containing all the enumerable property values
  • Object.entries(obj) returns an array of [key, value] pairs

Think of it like this: if your object is a filing cabinet, Object.keys() gives you the label on each folder, Object.values() gives you the contents of each folder, and Object.entries() gives you both the label and contents stapled together.

Object.values()

const user = {
  name: 'Alice',
  age: 30,
  role: 'developer'
};

const values = Object.values(user);
console.log(values); // ['Alice', 30, 'developer']

Notice you get the values in the same order as Object.keys() would return the keys.

Object.entries()

const user = {
  name: 'Alice',
  age: 30,
  role: 'developer'
};

const entries = Object.entries(user);
console.log(entries);
// [['name', 'Alice'], ['age', 30], ['role', 'developer']]

Each entry is a two-element array: [key, value]. This is particularly useful when you need both pieces of information during iteration.

When to Use Each

Use Object.values() when you only care about what's stored, not the property names. Use Object.entries() when you need to work with both keys and values together—perfect for transforming objects or building new data structures.

Key Takeaway: Object.values() extracts just the values as an array, while Object.entries() gives you [key, value] pairs—both return arrays of enumerable own properties in the same order as Object.keys().