Object.keys(), Object.values(), and Object.entries() in JavaScript
JavaScript provides three powerful methods for working with objects:
Object.keys(obj)→ Returns an array of keysObject.values(obj)→ Returns an array of valuesObject.entries(obj)→ Returns an array of key-value pairs
1. Object.keys(obj) – Get Keys
This method returns an array of all the keys (property names) of an object.
Example
Use Case
You can use Object.keys() to loop through an object:
Output:
2. Object.values(obj) – Get Values
This method returns an array of all the values in an object.
Example
Use Case
Calculate the total sum of an object’s numeric values:
3. Object.entries(obj) – Get Key-Value Pairs
This method returns an array of key-value pairs, where each item is a sub-array [key, value].
Example
Use Case
Loop through an object using forEach():
Convert an Object into a Map
When to Use Each Method?
| Method | Returns | Use Case |
|---|---|---|
Object.keys(obj) | Array of property names | Looping through keys, checking object properties |
Object.values(obj) | Array of property values | Summing values, filtering data |
Object.entries(obj) | Array of [key, value] pairs | Converting to Map, iterating with both keys and values |
Summary
✔ Object.keys() → Extracts keys from an object.
✔ Object.values() → Extracts values from an object.
✔ Object.entries() → Extracts key-value pairs from an object.
These methods are useful for iterating over objects, transforming data, and working with dynamic keys! Let me know if you need more examples. š

