How to Get the Last Key, Value, or Entry from an Object in JavaScript
Unlike arrays, JavaScript objects are fundamentally unordered collections of key-value pairs. However, since ES2015, the order of an object's keys is preserved in a predictable, chronological way for most operations. This allows us to reliably access the "last" property that was added to an object.
This guide will teach you how to get the last key, the last value, or the last [key, value] pair from an object by first converting its properties into an array.
The Core Concept: Predictable Key Order
To access a property by its position, you must first convert the object's properties into an array, which is indexed. The modern Object static methods (.keys(), .values(), .entries()) return arrays with a consistent insertion order for non-numeric keys.
Once you have an array, you can use modern array methods like .pop() or .at(-1) to get the last element.
How to Get the Last Value of an Object
The Object.values() method returns an array of an object's own enumerable property values.
For example, you have an object and need to get its last value without knowing the key's name.
// Problem: How to get the value 30?
const user = { name: 'Alice', age: 30 };
Recommeded Solution: at(-1)
The Array.prototype.at() method with a negative index is the most modern and readable way to get the last element of an array.
const user = { name: 'Alice', age: 30 };
// 1. Get an array of the object's values.
const values = Object.values(user); // -> ['Alice', 30]
// 2. Get the last element of the array.
const lastValue = values.at(-1);
console.log(lastValue); // Output: 30
Alternative Solution:.pop()
The Array.prototype.pop() method also works, but it mutates (modifies) the array it's called on, which can be an undesirable side effect.
const values = Object.values(user);
const lastValue = values.pop(); // This removes the last element from the `values` array
console.log(lastValue); // Output: 30
For simply reading the value, .at(-1) is the superior, non-destructive choice.
How to Get the Last Key of an Object
The Object.keys() method returns an array of an object's own enumerable property keys.
Solution:
const user = { name: 'Alice', age: 30, country: 'Canada' };
const keys = Object.keys(user); // -> ['name', 'age', 'country']
const lastKey = keys.at(-1);
console.log(lastKey); // Output: "country"
How to Get the Last [Key, Value] Pair (The Entry)
If you need both the key and the value of the last property, the Object.entries() method is the right tool. It returns an array of the object's [key, value] pairs.
Solution:
const user = { name: 'Alice', age: 30 };
// 1. Get an array of [key, value] pairs.
const entries = Object.entries(user);
/* -> [
[ 'name', 'Alice' ],
[ 'age', 30 ]
] */
// 2. Get the last entry from the array.
const lastEntry = entries.at(-1);
console.log(lastEntry); // Output: ['age', 30]
// 3. You can use array destructuring for a cleaner result.
const [lastKey, lastValue] = lastEntry;
console.log(`Last Key: ${lastKey}, Last Value: ${lastValue}`);
// Output: Last Key: age, Last Value: 30
Conclusion
While JavaScript objects are not natively indexed, you can reliably access their last property by first converting them into an array.
- To get the last value, the best practice is
Object.values(obj).at(-1). - To get the last key, use
Object.keys(obj).at(-1). - To get both the last key and value, use
Object.entries(obj).at(-1). - The
.at(-1)method is the recommended modern approach for getting the last element from an array, as it is highly readable and does not mutate the array.