Object.keys
Returns an array of an object's property names.
Available since: ES5
Syntax
Object.keys(obj)Parameters
| Parameter | Type | Description |
|---|---|---|
| obj | object | The object whose properties to list. |
Return value
ArrayAn array of strings with the property names.
Examples
Property names:
console.log(Object.keys({ name: 'Anna', age: 25 }).join(', '));Output:
name, ageThe companion Object.values() returns the values:
console.log(Object.values({ name: 'Anna', age: 25 }).join(', '));Output:
Anna, 25Notes
Object.keys(obj).length is a handy way to count an object's properties.
See also
- Object.entries — Returns an array of property name and value pairs.
- Object.values — Returns an array of an object's property values.
- Object.hasOwn — Checks whether an object has its own property.
Related recipes
In the tutorial
- Iterating objects and Object functions — How to iterate over an object in JavaScript: Object.keys, Object.values, Object.entries, Object.fromEntries, for...in and Object.hasOwn.