Code Logo

List Properties with Object.keys()

Published at25 Jul 2026
JavaScript Data Structures Easy 0 views
Like0

Write a JavaScript function that takes an object and returns an array of its property names (keys) using Object.keys(). The keys are returned in the same order as they appear in the object.

Object.keys() is a static JavaScript method that returns an array of a given object's own enumerable property names. It only includes properties directly on the object (not inherited through the prototype chain) and only enumerable properties. The order of keys follows ES2015 rules: integer-like keys in ascending order, followed by string keys in insertion order.

Object.keys() is part of a trio of reflection methods: Object.keys() returns property names, Object.values() returns property values, and Object.entries() returns both as [key, value] pairs. These methods are essential for iterating over objects, especially when combined with forEach() or for-of loops.

Time complexity is O(n) where n is the number of own enumerable properties. Space complexity is O(n) for the resulting array. The method does not modify the original object.

Edge cases include empty objects (returns empty array), objects with non-enumerable properties (not included), objects with symbol-keyed properties (not included in keys()), and null/undefined arguments (throws TypeError).

Example Input & Output

Example 1
Input
{"x":10}
Output
["x"]
Explanation

Single key

Example 2
Input
{"z":1,"y":2}
Output
["z","y"]
Explanation

Non-integer keys preserve insertion order

Example 3
Input
{}
Output
[]
Explanation

Empty object

Example 4
Input
{"name":"Alice","age":30}
Output
["name","age"]
Explanation

String keys

Example 5
Input
{"a":1,"b":2}
Output
["a","b"]
Explanation

Two keys a and b

Algorithm Flow

Recommendation Algorithm Flow for List Properties with Object.keys()
Recommendation Algorithm Flow for List Properties with Object.keys()

Solution Approach

function solution(obj) {
  return Object.keys(obj);
}

Best Answers

javascript - Approach 1
function solution(obj) {
  return Object.keys(obj);
}