inspection

Object.keys

Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.

Object.keys(obj)

Parameters

Parameter Purpose
obj The object whose enumerable own string-keyed properties are to be returned
returns Array of own enumerable string property names

Examples

console.log(Object.keys({ a: 1, b: 2, c: 3 }));

Logs [ 'a', 'b', 'c' ]

const arr = ['x', 'y', 'z'];
console.log(Object.keys(arr));

Logs [ '0', '1', '2' ] — array indices as strings

const o = Object.create({ inherited: 1 }, { own: { value: 2, enumerable: true } });
console.log(Object.keys(o));

Logs [ 'own' ] — inherited properties are skipped

const s = Symbol('id');
console.log(Object.keys({ [s]: 1, name: 'Ada' }));

Logs [ 'name' ] — Symbol keys are excluded

Gotcha

Returns only OWN enumerable STRING keys — inherited properties, non-enumerable properties, and Symbol keys are excluded. Use Reflect.ownKeys() to get everything including Symbols and non-enumerables.

Related methods

← All JS Object methods · Array methods · Spread vs rest