HOWTO · JavaScript
How to Get the First Key Name of an Object in JavaScript
In this article, we will see how to get the first key name of an object using Object.keys() and Object.entries() in JavaScript.
On this page
To get the first key of a JavaScript object, read the first element of the array returned by Object.keys(): Object.keys(obj)[0]. For an object such as { foo: 'bar' }, that expression evaluates to 'foo'. When you also need the matching value, Object.entries() offers a related route. This article walks through both approaches, explains which property actually counts as first, and shows what the expression returns when an object has no eligible properties.
Get the First Key of a JavaScript Object
JavaScript objects store data as key-value pairs, but the language has no dedicated function for asking which key comes first. The standard pattern is to build an array view of the object’s own properties and index into it. Two built-in functions do that job:
Object.keys(obj)returns an array containing only the property names, soObject.keys(obj)[0]is the most direct way to read the first key.Object.entries(obj)returns an array of[key, value]pairs, so the first key isObject.entries(obj)[0][0].
Both are static functions on the Object constructor. They are not instance methods on the object itself, and they only see the object’s own enumerable string-keyed properties.
Use Object.keys() to Get the First Key
Object.keys() takes a single argument — the object itself — and returns an array of its own enumerable string-keyed property names in the object’s property order. Index 0 of that array is the first key.
Run this example:
const obj = {
foo: 'bar',
baz: 42,
man: true
};
const keys = Object.keys(obj);
console.log(keys);
console.log(keys[0]);
[ 'foo', 'baz', 'man' ]
foo
The function returns names only. If you also need the value that belongs to the first key, look it up with obj[keys[0]], or reach for Object.entries() as shown next.
Use Object.entries() to Get the First Key
Object.entries() also takes the object as its single argument, but each element of the returned array is itself a two-element [key, value] array. That adds one indexing step: entries[0] is the first pair, and entries[0][0] is its key.
const obj = {
foo: 'bar',
baz: 42,
man: true
};
const entries = Object.entries(obj);
console.log(entries);
console.log(entries[0][0]);
[ [ 'foo', 'bar' ], [ 'baz', 42 ], [ 'man', true ] ]
foo
Choose Object.entries() when the value matters too, because entries[0][1] hands it to you in the same expression. For a name-only lookup, Object.keys() keeps the code shorter. Both arrays follow the same property order, so the two approaches always agree on which key is first.
Account for Property Order and Empty Objects
“First” does not always mean “first written”. For an object’s own enumerable string keys, JavaScript orders properties in two groups: keys that look like array indices — canonical numeric strings such as '2' or '10' — are sorted in ascending numeric order ahead of all other string keys, and the remaining string keys keep their insertion order.
const scores = {
b: 'bravo',
'10': 'ten',
a: 'alpha',
'2': 'two'
};
console.log(Object.keys(scores));
console.log(Object.keys(scores)[0]);
[ '2', '10', 'b', 'a' ]
2
Although b was declared first in the source, '2' comes back first because integer-index-like keys are grouped ahead of ordinary string keys. Symbol-keyed properties never appear in either array, and neither do inherited properties or non-enumerable ones, such as properties defined with Object.defineProperty and enumerable: false.
When an object has no eligible properties, the returned array has length zero, and indexing into it yields undefined rather than throwing an error:
const obj = {};
console.log(Object.keys(obj));
console.log(Object.keys(obj)[0]);
[]
undefined
If a missing key would be a problem for your code, check keys.length or test the result before using it.
Key Takeaways
Object.keys(obj)[0]returns the first own enumerable string key of an object.Object.entries(obj)[0][0]returns the same key and also gives access to the first value throughObject.entries(obj)[0][1].- Which key is first follows JavaScript’s property order: integer-index-like keys sort numerically before other string keys, so it is not necessarily the property you wrote first.
- Inherited, non-enumerable, and symbol-keyed properties are skipped entirely, and an object with no eligible keys produces
undefined.