How to access the first property of a Javascript object?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want the "first property" of a JavaScript object, the first question is what you mean by "first". Modern JavaScript does define property iteration order for ordinary objects, but objects are still not the best abstraction when your logic truly depends on stable insertion order. In many cases, Map is a better fit.
The Usual Answer: Object.keys(obj)[0]
If you want the first own enumerable string-keyed property according to normal object enumeration order, the most common pattern is:
This is simple and explicit. Object.keys gives you an array, and the first element of that array is the first enumerable own property key.
Understand What Order You Are Actually Getting
JavaScript object property order is not just "insertion order" in every case. For ordinary objects, integer-like keys are treated specially and come before regular string keys.
That will not necessarily preserve the visual order you typed in the source. Integer-like keys are ordered numerically before ordinary string keys.
So if your object looks like a dictionary with numeric keys, "first" may not mean what you expect.
for...in Works, but It Is Usually Not the Best Choice
You can also grab the first enumerated property with a loop:
This works, but it is usually less clear than Object.keys(obj)[0], and it requires you to think about inherited properties.
If Order Really Matters, Consider Map
If your logic depends on reliably taking the first inserted item, a Map is often the better abstraction.
With Map, stable insertion order is part of the design. With plain objects, property order exists, but objects are still primarily about named fields rather than ordered records.
Beware of Empty Objects
Whatever technique you use, handle the empty-object case.
If you skip that check, you may end up reading obj[undefined], which is rarely what you intended.
Symbols and Non-Enumerable Properties Are Separate
Object.keys only returns own enumerable string-keyed properties. It ignores:
- symbol keys
- non-enumerable properties
- inherited properties
If you need a different set of properties, choose a different reflection API such as Object.getOwnPropertyNames or Reflect.ownKeys.
So the right answer depends on which property space you are traversing.
Common Pitfalls
- Assuming "first property" is always just source insertion order.
- Forgetting that integer-like keys are ordered specially on ordinary objects.
- Using
for...inwithout filtering inherited properties. - Ignoring the empty-object case.
- Using plain objects when ordered entries would be modeled better as a
Map.
Summary
- For ordinary own enumerable properties,
Object.keys(obj)[0]is the usual answer. - The corresponding value is
obj[firstKey]. - Property order exists in modern JavaScript, but integer-like keys are treated specially.
- If order is fundamental to the data model,
Mapis often a better choice than an object. - Be explicit about empty objects, inherited properties, and symbol keys.
Related reading
- How to add 30 minutes to a JavaScript Date object?
- How to add new elements to an array?
- How to align a <div> to the middle (horizontally/width) of the page
- How to align content of a div to the bottom
- How to align text input correctly in react native?
- How to allow only numeric (0-9) in HTML inputbox using jQuery?
- How to always show scrollbar
- How to apply CSS to iframe?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.