Have you heard about Hack Frontend Community?Join us on Telegram!
Practice JS Problems

Object.keys, Object.values, Object.entries in JavaScript

JavaScript provides three convenient methods for working with objects:

MethodDescriptionReturns
Object.keys()Get all keys of objectstring[]
Object.values()Get all values of objectany[]
Object.entries()Get all pairs[key, value][]

Get All Keys

const user = { name: "Alice", age: 25, role: "admin" };

const keys = Object.keys(user);
console.log(keys); // ["name", "age", "role"]

Get All Values

const values = Object.values(user);
console.log(values); // ["Alice", 25, "admin"]

Get Key-Value Pairs

const entries = Object.entries(user);
console.log(entries);
// [["name", "Alice"], ["age", 25], ["role", "admin"]]

Iterating Over Object

Using for...of and Object.entries():

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

Using for...in (less recommended):

for (const key in user) {
  if (user.hasOwnProperty(key)) {
    console.log(`${key}: ${user[key]}`);
  }
}

for...in enumerates all enumerable properties, including inherited ones. So you need to use hasOwnProperty.

Important:

Order of keys in object is not strictly guaranteed (but in practice — stable in modern browsers).

Summary

  • Object.keys() — get keys
  • Object.values() — get values
  • Object.entries() — get key-value pairs

These are basic methods for everyday work with objects in JavaScript.

Practice JS Problems

By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.