HOWTO · JavaScript
How to Get Dictionary Length in JavaScript
Learn how to count an object's own enumerable string keys in JavaScript with Object.keys(), Object.entries(), or for...in, and how that differs from an array's length.
On this page
In JavaScript, a dictionary is usually represented by an object whose properties map keys to values. To find its length, first decide which properties you mean: JavaScript objects do not have a built-in length property, so the usual count is the number of the object’s own enumerable string-keyed properties. Arrays are different: they have a built-in length property for their indexed elements.
What Does an Object’s Length Mean in JavaScript?
An object can have own properties, inherited properties, enumerable or non-enumerable properties, and symbol-keyed properties. There is no single built-in object length that counts all of these. For a typical dictionary, counting own enumerable string keys is the useful default. The methods below use that same scope, while for...in needs an ownership check to exclude inherited enumerable keys.
Use Object.keys() to Count Own Enumerable String Keys
Object.keys(object).length is the concise choice when you only need a count or a list of keys. Object.keys() returns an array of the object’s own, enumerable, string-keyed property names; its array length is the count. Inherited properties, symbol keys, and non-enumerable properties are not included.
Run this example:
const dictionary = {
name: "John",
age: 30,
city: "New York"
};
console.log(Object.keys(dictionary).length);
The expression counts the three keys in this object. The example is supplied as code; no runtime output is included here. An object with no own enumerable string keys produces a count of 0, so checking whether that kind of dictionary is empty can be written as Object.keys(dictionary).length === 0.
Use Object.entries() When You Need Key-Value Pairs
Object.entries(object).length counts the same own, enumerable, string-keyed properties as Object.keys(object).length. The difference is that Object.entries() returns an array of [key, value] pairs. Use it when the next step needs both parts, such as formatting each entry or transforming values; if you only need the count, Object.keys() communicates that intent more directly.
For example, given { name: "Alice", age: 25 }, the entries are conceptually [["name", "Alice"], ["age", 25]], and the entries array has two elements. This describes the returned structure; no execution result is asserted.
Count Properties with a for…in Loop
A for...in loop visits enumerable string keys on the object and along its prototype chain. If you want the same own-property count as the methods above, guard each key. Calling Object.prototype.hasOwnProperty.call() is safer than object.hasOwnProperty(key): an object may have its own property named hasOwnProperty that shadows the method.
const dictionary = { name: "Bob", age: 40, city: "Chicago" };
let count = 0;
for (const key in dictionary) {
if (Object.prototype.hasOwnProperty.call(dictionary, key)) {
count++;
}
}
console.log(count);
This loop counts own enumerable string keys and skips inherited enumerable keys. The exact protected term hasOwnProperty refers to the ownership check used here. Use the loop when you need to perform work as each key is visited; otherwise, Object.keys() or Object.entries() is usually simpler to read.
Choose an Approach and Handle Common Cases
Choose Object.keys(object).length for a straightforward count or when you also need the keys. Choose Object.entries(object) when you need key-value pairs as well as their count. Choose a guarded for...in loop when a manual iteration fits the surrounding logic. These approaches describe what is counted; they do not establish a performance ranking for every object or workload.
A nested object is still one property in its parent. For example, { user: { name: "Sam" } } has one top-level key, user; counting keys inside user is a separate operation. A recursive count would need an explicit definition of which nested values to visit and is not provided by these methods automatically.
Arrays already provide array.length, which represents their indexed length and can include positions without assigned values. Object.keys(array).length instead counts the array’s own enumerable string keys, so it is not a substitute for the array length when there are holes or additional properties. Use the built-in array property for array length and the object-key methods for dictionary-style property counts.