HOWTO · JavaScript

How to Get Key of a JavaScript Object

This tutorial details how to get key of an object in javascript.

On this page

Use Object.keys(obj) to get an array of an object’s own enumerable string property names. Use Object.entries(obj) when each key should stay paired with its value. Both methods leave the object unchanged. For a loop that can also visit inherited enumerable properties, use for...in and filter for own properties when that is what your task requires.

Get Object Keys with Object.keys()

Object.keys() returns an array of own, enumerable string keys. It is useful when the property names are not known in advance, such as when processing a record returned by an API. The returned array contains names only; read a value with obj[key]. If an object has no own enumerable string properties, the result is an empty array. Symbol properties are not included.

Property order is defined, but it is not always the order in which keys were written. Array-index keys (canonical integer index strings from 0 through 4294967294) appear first in ascending numeric order. Other string keys follow in property creation order. A string such as "01" is not an array index, so it stays with the other string keys. Arrays work the same way: Object.keys() returns their present enumerable index keys as strings; it does not return array values.

Run this example:

const record = { 4: 'pear', 2: 'mango', name: 'fruit', '01': 'label' };
const keys = Object.keys(record);
console.log(keys);
console.log(record[keys[0]]);
console.log(Object.keys(['red', 'blue']));

This example shows numeric index keys before other strings, keeps "01" among ordinary string keys, and uses a returned key to look up its value. Getting the keys does not reorder or rewrite the properties in record.

Get Object Keys and Values with Object.entries()

Use Object.entries(obj) when the next step needs each key and its corresponding value. It returns an array of two-item arrays shaped like [[key, value], ...]. Like Object.keys(), it includes only the object’s own enumerable string properties, uses the same property order, and excludes symbols. It does not recursively walk values that happen to be objects.

Destructuring makes it convenient to process each pair. The following complete example prints a label and value for each property:

const settings = { theme: 'dark', language: 'English' };
for (const [key, value] of Object.entries(settings)) {
  console.log(`${key}: ${value}`);
}

Choose Object.keys() if names alone are enough, and Object.entries() if the loop needs names and values together. If a key is already known, there is no need to enumerate the object; access the value directly with settings.theme or settings['theme'].

Iterate Over Object Keys with for…in

A for...in loop visits enumerable string properties. It can include properties inherited through the object’s prototype, so its results can differ from Object.keys(), which lists only own properties. Use Object.hasOwn(obj, key) inside the loop when you need to exclude inherited properties. This method is available in modern JavaScript; for older environments, use Object.prototype.hasOwnProperty.call(obj, key).

const parent = { category: 'fruit' };
const item = Object.create(parent);
item.name = 'mango';

for (const key in item) {
  if (Object.hasOwn(item, key)) {
    console.log(`${key}: ${item[key]}`);
  }
}

The loop can encounter both name and the inherited category, while the own-property check prints only name. If you want all enumerable properties, omit the check. For a predictable list of an object’s own enumerable string keys, Object.keys() is usually the clearest choice.

Enumeration is shallow. If a top-level property contains another object, the key list contains the top-level property’s name, and its value remains that nested object. To list nested keys, call Object.keys() on that value separately; recursive traversal requires additional logic to walk each nested object. Choose among the three approaches by the properties you need: Object.keys() for names, Object.entries() for name-value pairs, and for...in for enumerable properties across the prototype chain.