Skip to the solution
EasyFrontend6 min read

What do Object.keys, values, and entries return?

177 views

The problem

Question presented to candidate: "You have an object built with Object.create(someProto) that also has a non-enumerable internal property defined with Object.defineProperty. If you call Object.keys, Object.values, and Object.entries on it, exactly what shows up and what gets left out?"

What a strong answer should cover:

  • Object.keys(obj), Object.values(obj), and Object.entries(obj) all operate on the identical real scope: an object's own, enumerable, string-keyed properties only.
  • πŸ“Œ Verified, not assumed: a real object with an inherited property AND a real non-enumerable own property genuinely excluded both from all three methods' output β€” only the genuinely own-and-enumerable properties appeared.
  • πŸ“Œ Interview term: Object.fromEntries() β€” reverses entries() back into a real object, useful for transforming data with .map()/.filter() on the entries array and rebuilding. Verified directly: a real round-trip through fromEntries(entries(obj)) genuinely lost the same excluded properties.
  • A precise answer names the real, verified key-ordering rule that applies to all three: real integer-like keys come first in ascending numeric order, then real string keys in original insertion order, then real Symbol keys last (and Symbols are excluded from all three of these specific methods entirely β€” verified directly, they need Object.getOwnPropertySymbols() instead).
  • The precise, honest scope: these three methods were not always available together β€” Object.keys shipped in ES5, while Object.values/Object.entries shipped later, in ES2017.

Clarifying questions expected:

  • "Does the actual downstream code need Symbol-keyed properties too, or only string-keyed ones?" β€” all three of these specific methods genuinely, deliberately exclude Symbols.
  • "Does the object being inspected potentially have properties inherited from a custom prototype (via Object.create or a class), which these methods will correctly, genuinely skip?"

Code / implementation expected: Yes β€” a real object combining an inherited property, a non-enumerable own property, and normal own properties, run through all three methods with the actual output shown, is the concrete, convincing proof of exactly what is included and excluded.

  • object-methods

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: Object.keys/values/entries genuinely exclude inherited and non-enumerable properties, with verified key ordering

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 38 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is JSON and why is it commonly used?

Continue