Member-only story
Accessing an Object’s Keys, Values, and Entries in JavaScript
Let’s dive into objects

Object.keys(obj)
→ returns an array of a given object’s property names.Object.values(obj)
→ returns an array of a given object's own property values.Object.entries(obj)
→ returns an array of a given object’s own string-keyed property[key, value]
pairs.
var user = { name : "John", profession : "👨⚕️", salary : 10000}Object.keys(user); // ["name", "profession", "salary"]Object.values(user); // ["John", "👨⚕️", 10000]Object.entries(user); //output
0: (2) ["name", "John"]
1: (2) [“profession”, “👨⚕️”]
2: (2) [“salary”, “10000]
length: 3
All the above three methods return only the enumerable property of the object.
var user = {
name : "John"
}Object.defineProperty( user , 'salary', {
value: 10000,
enumerable: false
});
In the above code, we define a property salary
and set enumerable
property as false
.This will make the salary
hidden from the Object.keys
.
Object.keys(user); ["name"]Object.values(user); ["John"]Object.entries(user); // ["name", "John"]
We cannot loop through the symbol
properties of the object.
var user = {
name : "John"
}
let Passcode = Symbol("Passcode");user[Passcode] = "1234";
Now we have set the symbol
as a property of the object, but this will not be accessible using the three methods above.
Object.keys(user); //["name"]Object.values(user); // ["John"]Object.entries(user); // ["name", "John"]
To access the symbol property of the object, we can use getOwnPropertySymbols()
.
Object.getOwnPropertySymbols(user); //[Symbol(Passcode)]