Skip to main content
Practice Problems

How to GET all keys and values of Object 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

ts
const user = { name: "Alice", age: 25, role: "admin" }; const keys = Object.keys(user); console.log(keys); // ["name", "age", "role"]

Get All Values

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

Get Key-Value Pairs

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

Iterating Over Object

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

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

Using for...in (less recommended):

ts
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.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems