Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For the usual count of an object’s own, enumerable string-keyed properties, use Object.keys(obj).length. Use a different API if you need to include non-enumerable properties, symbol keys, or inherited properties.

Choose what you mean by “properties”

JavaScript objects do not have a built-in length value for their properties. The right count depends on whether you mean own or inherited properties, enumerable or non-enumerable properties, and string keys or symbol keys.

What you want to count Code Included keys
Usual object-data count Object.keys(obj).length Own, enumerable string keys
All own string properties Object.getOwnPropertyNames(obj).length Enumerable and non-enumerable own string keys
Every own property key Reflect.ownKeys(obj).length Enumerable and non-enumerable own string and symbol keys
Enumerable strings on the object and its prototype chain for...in Own and inherited enumerable string keys

MDN’s Object.keys reference defines its result as an array of an object’s own enumerable string-keyed property names. Its property ownership and enumerability guide explains how the other methods differ.

Count the usual object properties

Use Object.keys() when you want the number of ordinary, enumerable string-keyed properties directly on the object:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const user = { name: "Ava", active: true };
const count = Object.keys(user).length;

console.log(count); // 2

Inherited properties are not included. Neither are non-enumerable properties or symbol-keyed properties. This makes Object.keys(obj).length a concise choice for typical data objects, but not a count of every possible property.

Include hidden or symbol-keyed properties

Use Object.getOwnPropertyNames() when non-enumerable string properties should count, or Reflect.ownKeys() when every own key—including symbols—should count:

const obj = {};
Object.defineProperty(obj, "hidden", { value: 1 });
const id = Symbol("id");
obj[id] = 2;
obj.visible = 3;

Object.keys(obj).length;                // 1: visible
Object.getOwnPropertyNames(obj).length; // 2: hidden, visible
Reflect.ownKeys(obj).length;            // 3: hidden, visible, symbol

Object.defineProperty() creates a non-enumerable property by default unless its descriptor sets enumerable: true. MDN documents Object.getOwnPropertyNames() as returning own string property names whether enumerable or not. Reflect.ownKeys() returns all own string and symbol keys, making it the single-call option for a complete own-key count.

Count inherited properties only when intended

A for...in loop visits enumerable string-keyed properties on the object and along its prototype chain. If you use it to count only the object’s own properties, check ownership before incrementing:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
let count = 0;
for (const key in obj) {
  if (Object.hasOwn(obj, key)) {
    count++;
  }
}

Object.hasOwn() is available in modern JavaScript. For older environments, Object.prototype.hasOwnProperty.call(obj, key) is an alternative. The loop still excludes symbols and non-enumerable properties; use the APIs above when those need to count.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Arrays and other edge cases

Arrays

For ["x", "y"], Object.keys(array).length counts the two enumerable index properties. Reflect.ownKeys(array).length also includes the array’s non-enumerable length property. Choose the method based on whether structural properties such as length belong in your count.

Null, undefined, and primitive values

Object.keys() throws a TypeError when passed null or undefined. Most other primitive values are coerced to objects; for example, strings expose enumerable index properties. If a function is intended to accept only objects, validate its input or document that requirement rather than assuming every argument is safe.

Property order

Order does not affect the count. For reference, Reflect.ownKeys() lists non-negative integer-index keys first in numeric order, then other string keys in creation order, followed by symbol keys in creation order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.