Question
What is the most efficient way to count the keys (properties) of a JavaScript object?
Can this be done without iterating through the object's properties, rather than using a loop such as:
let count = 0;
for (const key in myobj) {
if (Object.prototype.hasOwnProperty.call(myobj, key)) {
count++;
}
}
Older Firefox versions provided a non-standard __count__ property, but it was removed. What is the standard JavaScript approach?
Short Answer
You will learn how to count an object's own enumerable properties with Object.keys(obj).length, why JavaScript must inspect properties to produce the count, and when alternatives such as Map.size are a better fit.
Concept
JavaScript objects do not have a standard built-in property count such as object.length or object.size.
For ordinary objects, the standard approach is:
const count = Object.keys(myobj).length;
Object.keys(myobj) creates an array containing the object's own enumerable string-keyed property names. The array's length is therefore the count.
const user = {
name: "Ava",
role: "admin",
active: true
};
console.log(Object.keys(user).length); // 3
This is usually clearer and less error-prone than writing a for...in loop yourself.
Why counting still requires work
Conceptually, JavaScript needs to determine which properties qualify for the count. There is no portable standard API that promises an instant count for all ordinary object properties. Even if a JavaScript engine internally tracks metadata, your code should use the standard API rather than relying on engine-specific implementation details.
Mental Model
Think of an object as a cabinet of labeled drawers.
- Own properties are drawers physically inside this cabinet.
- Inherited properties are drawers available through a cabinet it was built from (its prototype).
- Enumerable properties are drawers included in the normal inventory list.
- Non-enumerable properties exist, but are intentionally left off that normal list.
Object.keys(cabinet) produces the normal inventory list for this cabinet. Then .length counts the labels on that list.
If you constantly need to know how many items are stored, an object is like a cabinet without a visible item counter. A Map is more like a collection with a built-in counter: map.size.
Syntax and Examples
The usual syntax is:
const count = Object.keys(object).length;
Example:
const scores = {
maya: 98,
liam: 87,
noah: 91
};
const numberOfScores = Object.keys(scores).length;
console.log(numberOfScores); // 3
Object.keys(scores) returns:
["maya", "liam", "noah"]
The array contains three names, so its .length is 3.
Empty object
const settings = {};
console.log(.(settings).);
Step by Step Execution
Consider this code:
const inventory = {
apples: 12,
oranges: 8,
pears: 5
};
const keys = Object.keys(inventory);
const count = keys.length;
console.log(count);
Step by step:
-
inventoryis created with three own enumerable properties:apples,oranges, andpears. -
Object.keys(inventory)examines the object and returns an array of its enumerable string keys:["apples", "oranges", "pears"] -
That array is assigned to
keys. -
keys.lengthis3, because the array has three elements.
Real World Use Cases
Counting object properties is useful when an object acts as a dictionary or lookup table.
Check whether a form has validation errors
const errors = {
email: "Enter a valid email address",
password: "Password is too short"
};
if (Object.keys(errors).length > 0) {
console.log("The form has errors.");
}
Count cached records
const cache = {
"user:101": { name: "Ava" },
"user:102": { name: "Omar" }
};
console.log(`Cached users: ${Object.keys(cache).length}`);
Determine whether an API result contains fields
const responseData = {};
if (Object.keys(responseData).length === 0) {
.();
}
Real Codebase Usage
In real applications, Object.keys(obj).length is common for occasional checks, especially with plain JSON-like data.
Prefer an emptiness check when that is all you need
If you only need to know whether an object has at least one own enumerable string property, this is readable:
function hasValues(value) {
return Object.keys(value).length > 0;
}
Use it after validating that value is an object when input may be null, an array, or another type.
Use guard clauses for validation
function saveProfile(updates) {
if (Object.keys(updates).length === 0) {
throw new Error("Provide at least one profile update.");
}
// Save the updates...
}
Track the count separately for frequently changing collections
If you add and remove records often and need the count repeatedly, do not repeatedly call . Use , whose is designed for this:
Common Mistakes
Expecting object.length to work
Plain objects do not automatically have a length property.
const user = { name: "Ava", role: "admin" };
console.log(user.length); // undefined
Use:
console.log(Object.keys(user).length); // 2
Using for...in without considering inherited properties
for...in visits enumerable properties from the object and its prototype chain.
const parent = { inherited: true };
const child = Object.create(parent);
child.own = true;
for ( key child) {
.(key);
}
Comparisons
| Approach | Counts | Includes inherited properties? | Best use |
|---|---|---|---|
Object.keys(obj).length | Own enumerable string keys | No | Standard count for ordinary objects |
Object.getOwnPropertyNames(obj).length | Own string keys, enumerable and non-enumerable | No | Include hidden string properties |
Reflect.ownKeys(obj).length | Own string and symbol keys, enumerable and non-enumerable | No | Inspect every own key |
for...in with ownership check | Own enumerable string keys | No, if checked | When processing each key manually |
for...in alone |
Cheat Sheet
// Standard: own enumerable string-keyed properties
const count = Object.keys(obj).length;
// Is a plain object empty in the Object.keys sense?
const isEmpty = Object.keys(obj).length === 0;
// Own string keys, including non-enumerable keys
const allStringKeyCount = Object.getOwnPropertyNames(obj).length;
// All own keys: strings, symbols, enumerable, and non-enumerable
const allKeyCount = Reflect.ownKeys(obj).length;
// Count symbols separately when needed
const enumerableStringAndSymbolCount =
Object.keys(obj).length + Object.getOwnPropertySymbols(obj).length;
// Safe ownership test in a for...in loop
Object.prototype.hasOwnProperty.call(obj, key);
// Modern ownership test
Object.hasOwn(obj, key);
// A Map exposes a collection size directly
countFromMap = map.;
FAQ
What is the simplest way to count object properties in JavaScript?
Use Object.keys(obj).length. It counts the object's own enumerable string-keyed properties.
Is there an object .size property in JavaScript?
Plain objects do not have .size. Map and Set have a .size property.
Does Object.keys() count inherited properties?
No. It returns only keys that belong directly to the object.
Does for...in count inherited properties?
Yes. A for...in loop visits enumerable properties from the prototype chain as well. Filter with an ownership check if you use it.
Does Object.keys() count symbol properties?
No. Use Object.getOwnPropertySymbols(obj) for symbols, or Reflect.ownKeys(obj) for all own keys.
Can I count object properties without iteration?
There is no standard, general-purpose property-count field on ordinary objects. Object.keys() is the standard API, and it must determine the matching keys.
Mini Project
Description
Build a small validation helper for a profile update endpoint. The helper receives an object of field errors and reports whether saving can continue. This demonstrates counting an object's keys without accidentally including inherited properties.
Goal
Create a function that returns a readable status message based on the number of validation errors in an object.
Requirements
Use Object.keys() to count validation error fields.
Return "No validation errors" when the object has no error fields.
Return a singular message for exactly one error.
Return a plural message for two or more errors.
Test the function with empty, one-error, and multiple-error objects.
Keep learning
Related questions
Abort Ajax Requests with jQuery jqXHR.abort()
Learn how to cancel an in-progress jQuery Ajax request with jqXHR.abort(), handle abort status safely, and avoid stale UI updates.
Access the Correct this Inside a JavaScript Callback
Learn why JavaScript this changes in callbacks and how to preserve an object context using bind, arrow functions, and event handler patterns.
Add Key-Value Pairs to JavaScript Objects
Learn how to add key-value pairs to JavaScript objects with dot and bracket notation, dynamic keys, examples, and common mistakes.