Javascript
Coding
Programming
Web Development
Javascript Objects

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.

Browse interview questions

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:

javascript
1const user = { age: 25, name: "John", role: "developer" };
2
3const firstKey = Object.keys(user)[0];
4const firstValue = user[firstKey];
5
6console.log(firstKey);
7console.log(firstValue);

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.

javascript
const obj = { b: "bee", 2: "two", a: "aye", 1: "one" };

console.log(Object.keys(obj));

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:

javascript
1const obj = { age: 25, name: "John" };
2
3let firstKey;
4for (const key in obj) {
5  if (Object.hasOwn(obj, key)) {
6    firstKey = key;
7    break;
8  }
9}
10
11console.log(firstKey);

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.

javascript
1const settings = new Map();
2settings.set("theme", "dark");
3settings.set("language", "en");
4
5const firstEntry = settings.entries().next().value;
6console.log(firstEntry);

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.

javascript
1const obj = {};
2const firstKey = Object.keys(obj)[0];
3
4if (firstKey === undefined) {
5  console.log("object has no properties");
6}

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.

javascript
1const sym = Symbol("id");
2const obj = { name: "John", [sym]: 42 };
3
4console.log(Object.keys(obj));      // ["name"]
5console.log(Reflect.ownKeys(obj));  // ["name", Symbol(id)]

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...in without 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, Map is often a better choice than an object.
  • Be explicit about empty objects, inherited properties, and symbol keys.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.