List Keys
Object.keys returns an array of an object's own enumerable keys.
It is useful for iteration and validation.
JavaScript Tutorial
Object.keys returns an array of an object's own enumerable property names.
It is commonly used for iteration and validation.
When working with objects, you often need to list keys for loops or checks.
Object.keys makes that simple and reliable.
Object.keys(obj)const user = { name: "Ava", role: "admin" };
const keys = Object.keys(user);
console.log(keys);Returns ['name', 'role'].
const settings = { theme: "dark", layout: "grid" };
Object.keys(settings).forEach((key) => {
console.log(key, settings[key]);
});Loop over keys to access values.
Object.keys returns an array of an object's own enumerable keys.
It is useful for iteration and validation.
Use Object.values for values and Object.entries for key-value pairs.
Together they cover most inspection use cases.
Object.keys works on arrays too, returning index strings.
It ignores properties in the prototype chain.
Without
for (const key in obj) {
if (Object.hasOwn(obj, key)) {
console.log(key);
}
}With
Object.keys(obj).forEach((key) => console.log(key));Object.keys returns only own enumerable properties.
Use Object.values when you only need values.
Key order exists but should not be relied on for logic.
An array of own enumerable keys.
No, only own properties.
It avoids inherited properties without extra checks.
Practice: Count how many properties an object has using Object.keys.
const user = { name: "Ava", role: "admin" };
// TODO: count keys
One Possible Solution
const user = { name: "Ava", role: "admin" };
const count = Object.keys(user).length;
console.log(count);An array of own enumerable property names.
No, only own properties.
Yes, it returns string indexes.
Try adding properties and see how the key list changes.