JavaScript

How to Count Number of Keys in Object in JavaScript

Objects are JavaScript’s non-primitive data type. It helps to store multiple sets of data in the form of key-value pairs. Keys are the properties of an object specified as a variable that is connected to the object. Counting the number of keys or properties of an object is the common problem encountered in JavaScript.

This blog post will define the procedure for counting the number of keys in an object in JavaScript.

How to Count the Number of Keys in an Object in JavaScript?

For counting the number of keys in an object, use the following methods:

  • Object.keys() with length property
  • Combination of the “for” loop with the “in” keyword

Let’s have a look at each of these approaches one by one!

Method 1: Counting the Number of Keys in an Object Using the Object.keys() Method

The “Object.keys()” method gives an array as an output with strings as its elements that correspond to the enumerated properties already existing on the object. The list of properties appears in the same order as when you manually loop through the object’s attributes. For counting the number of keys in an object, the “Object.key()” method is utilized with the “length” property.

Syntax
Use the below-given syntax, to count the number of keys in an object:

Object.keys(object).length
  • keys()” is the method of the “Object” type and the “object” is an argument whose keys/properties will be counted.
  • It returns the count of the number of keys in the object.

Example
First, create an object named “info” with four properties “firstName”, “lastName”, “age” and “contact” in the key-value pair:

const info = {
 firstName: 'Stephen',
 lastName: 'Cain',
 age: 28,
 contact: 090394809
};

Call the “Object.keys()” method with the “length” property passing an object “info” as an argument that will count the keys in an object:

console.log(Object.keys(info).length);

The output displays “4” which is the count of keys in the object “info”:

If you want only the names of keys in an object instead of count, simply call the “Object.keys()” method by passing an object as an argument:

console.log(Object.keys(info));

The output displays the names of the keys in the object in an array:

Method 2: Counting the Number of Keys in Object Using “for” Loop with “in” Keyword

The keys of an object are also counted using the “for” loop with the “in” keyword. The “for” loop iterates over the object’s properties and the “in” keyword retrieves the properties from it. To count the number of keys in an object using the “for_in” loop, follow the below syntax that will be used in the examples below.

Syntax

for(var key in Object) {
 .........
}

Example 1: Count Number of Keys in an Object
For counting the number of keys in an object, consider the same object “info” created in the above example. Then, create a variable “count” and assign a value 0 to it:

var count = 0;

Pass the object “info” to the for_in loop:

for(var key in info) {
 count++;
}

Finally, print the count of keys in the object using “console.log()” method:

console.log(count);

Output

Example 2: Count Number of Keys in an Inherited Objects
The “for” loop also counts inherited properties. Here, we will create an object “employee” with two properties “gender”, and “joiningDate” that will inherit from the object “info”:

const employee = {
 gender: 'male',
 joiningDate: 'Jan,2020'
};

Inherit the object “employee” with “info” using object’s property “__proto__”:

info.__proto__ = employee

Pass the object “info” to the “for_ in” loop and increment the count on each iteration. It will also count the properties of the object “employee” because it inherits from the object “info”:

for(var key in info) {
 count++;
}

The output “6” indicates that this approach counts the keys of both objects’ “info” and its inherited object “employee”:

Example 3: Count Number of Keys in a Child Object
If you simply want to get the keys of the child object “employee”, use the “hasOwnProperty()” method inside the loop:

for(var key in employee) {
 if (employee.hasOwnProperty(key)) {
  count++;
 }
}

Output

Conclusion

To count the number of keys in an object in JavaScript, use the “length” property of the JavaScript “Object” type method “keys()” as “Object.keys()” or the combination of “for” loop with the “in” keyword. The “for_in” approach also counts inherited properties while the “Object.keys()” method does not count the linked properties; it just counts the object’s own properties. In this blog post, we defined the procedure for counting the number of keys in an object in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.