Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Object.keys

Object.keys() returns an array of an object's own enumerable string-keyed property names. It excludes inherited and non-enumerable properties.

How it works

Object.keys() examines the supplied value and returns a new array containing the names of its own enumerable properties when those names are strings. It excludes inherited and non-enumerable properties, preserves the property-enumeration order, and handles primitive inputs through object coercion except for null and undefined, which cause a TypeError.

Syntax

Object.keys(obj)

Parameters

NameTypeDescription
objobjectThe value whose own enumerable string-keyed property names should be listed.

Returns

An array of strings representing the given object's own enumerable string-keyed property keys.

Key points

  • It produces an array containing only the target object's own enumerable property names whose keys are strings.
  • Key order follows JavaScript's property-enumeration order as observed by for...in.
  • Choose Object.values() when the required result is the corresponding property values rather than their names.
  • Use Object.entries() when each result must retain its key together with its value.
  • Object.getOwnPropertyNames() is the broader choice when non-enumerable own string properties must be included.

Common mistakes

  • Passing null or undefined throws a TypeError instead of returning an empty array.
  • Primitive inputs do not behave uniformly: a string contributes index-like keys, while other non-null primitives produce no keys.
  • Unlike for...in, Object.keys() does not include enumerable properties inherited through the prototype chain.

Example: Iterate over an object's keys

const user = { name: "Ada", age: 36 };

for (const key of Object.keys(user)) {
  console.log(key, user[key]);
}

Iterates over the object's own enumerable string-keyed properties and uses each key to read its value.

When to use it

List own enumerable properties

Enumerate an object's directly declared public fields without traversing its prototype chain.

Process keys functionally

Build a new collection by transforming or filtering property names.

Inspect object shape

Validate or inspect the available enumerable fields in configuration or input data.

Interview questions

QWhat does Object.keys() return?

Object.keys() returns an array of an object's own enumerable string-keyed property names. It does not include inherited, non-enumerable, or Symbol-keyed properties.

Follow-up: How would you include non-enumerable string properties? Use Object.getOwnPropertyNames(), which returns both enumerable and non-enumerable own string properties.

QHow are the returned keys ordered, and what do they contain?

The returned array uses the same property order exposed by a for...in loop for the object's own enumerable string properties. The key array contains strings, not values.

Follow-up: How can you obtain keys and values together? Use Object.entries(), which returns key-value pairs.

QHow does Object.keys() handle primitive arguments?

The argument is coerced to an object unless it is null or undefined. Strings can yield enumerable index keys, whereas other primitive values produce an empty array.

Follow-up: What happens with Object.keys(null)? It throws a TypeError because null cannot be coerced to an object.

  • Object.values()
  • Object.entries()
  • Object.getOwnPropertyNames()
  • for...in

Source: Object.keys () - JavaScript | MDN. Checked 2026-10-15.