How to Count the Number of Properties in a JavaScript Object

Learn how to count the number of properties in a JavaScript object, with two different methods.

To count the number of JavaScript object properties, you can either use:

  • a for loop
  • or the Object.keys() method.

Let’s explore them both!

Count object properties with a for loop

Here’s a JavaScript object called dog:

const dog = {
  name: "Naya",
  age: 2,
  color: "black",
  Breed: "Rottweiler mix",
}

To count the number of object properties in dog, let’s first declare a variable called count and give it a start value of 0:

let count = 0

Now we need to loop through the dog object and for each property we come across we add one (+ 1) to the count variable:

for (let properties in dog) {
  count = count + 1
}

Now try printing the result by using console.log():

console.log(count)
// Result: 4

If you wrote the code correctly, you should get 4.

Count object properties with Object.keys()

You can also use JavaScript’s Object.keys() method to count all the enumerable properties (more about that in a sec) on an object.

Let’s reuse the dog object from before, but now we pass the dog object to Object.keys() and calculate its length with the length property:

const dog = {
  name: "Naya",
  age: 2,
  color: "black",
  Breed: "Rottweiler mix",
}

let count = Object.keys(dog).length

console.log(count)
// Result: 4

As you can see, the result is the same. Or is it?

Yes and no. It depends on the context.

for vs. Object.keys()

The difference between using a for loop and Object.keys() is:

  • The for loop counts both the properties of the object (here, dog) and any properties that might be linked to the object outside of it.
  • By default, the Object.keys() method only counts (enumerates) the object’s (own) enumerate properties — not linked properties.

Enumeration is another word for counting.

What do I mean with “linked properties”?

Let’s take the same Object.keys() example from before, but this time we link the dog object to an object called animal by using the _proto_ property:

// New object
let animal = {
  fourLegs: true,
}

const dog = {
  name: "Naya",
  age: 2,
  color: "black",
  Breed: "Rottweiler mix",
}

// Link dog object to animal object
dog.__proto__ = animal

var count = Object.keys(dog).length
console.log(count)
// Result: 4

Why is the result 4, when the total number of properties of dog and animal is 5, and we just linked them together and counted them?

Because we’re using Object.keys() which only counts its own (enumerable) properties, not linked properties. Therefore the fourLegs property from the animal object doesn’t get counted in the example above.

Now let’s try another example, but this time we use the for loop to count properties:

const animal = {
  fourLegs: true,
}

const dog = {
  name: "Naya",
  age: 2,
  color: "black",
  Breed: "Rottweiler mix",
}

// Link dog object to animal object
dog.__proto__ = animal

let count = 0

for (properties in dog) {
  count = count + 1
}

console.log(count)
// Result: 5

Now we’re getting all 5 properties, because unlike Object.keys() when we use a for loop we count every property of an object, including linked object properties.

As you can see, it matters which approach you use.

In summary:

  • There are two ways to count the number of properties in an object. You can use a for loop or the Object.keys() method.
  • Use a for loop if you wish to include any linked object properties in your property count.
  • Use Object.keys() if you only wish to count enumerable properties (the object’s own).

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊

Kofi

Share & Discuss on