Object.keys()
Object.keys(obj) Returns:
Array · Updated March 13, 2026 · Object Methods javascript object keys enumerable
Object.keys() returns an array of a given object’s own enumerable string-keyed property names, in the same order as a for...in loop (except for...in also enumerates inherited properties).
Syntax
Object.keys(obj)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| obj | Object | required | The object whose own enumerable string-keyed property names are to be returned. |
Return Value
An array of strings representing the given object’s own enumerable string-keyed property names.
Examples
Basic usage
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj));
// ['a', 'b', 'c']
With an array
const arr = ['a', 'b', 'c'];
console.log(Object.keys(arr));
// ['0', '1', '2']
Order of keys
const obj = { 10: 'ten', 1: 'one', 2: 'two' };
console.log(Object.keys(obj));
// ['1', '2', '10'] — numeric keys sorted, then string keys in insertion order
Non-enumerable properties are excluded
const obj = Object.create({}, {
hidden: { value: 'secret', enumerable: false },
visible: { value: 'public', enumerable: true }
});
console.log(Object.keys(obj));
// ['visible']
Primitive values are coerced
Object.keys('hello');
// ['0', '1', '2', '3', '4']
Object.keys(42);
// []
Object.keys(null);
// throws TypeError
Common Patterns
Iterating over object properties
const user = { name: 'Alice', age: 30, role: 'admin' };
Object.keys(user).forEach(key => {
console.log(`${key}: ${user[key]}`);
});
// name: Alice
// age: 30
// role: admin
Checking if an object is empty
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
console.log(isEmpty({})); // true
console.log(isEmpty({ a: 1 })); // false
Transforming keys
const original = { firstName: 'Alice', lastName: 'Smith' };
const snakeCase = Object.keys(original).reduce((acc, key) => {
const snakeKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
acc[snakeKey] = original[key];
return acc;
}, {});
console.log(snakeCase);
// { first_name: 'Alice', last_name: 'Smith' }
See Also
- Object.entries() — returns [key, value] pairs
- Object.values() — returns an array of values
- Object.assign() — copies properties between objects