/

How to Count the Number of Properties in a JavaScript Object

How to Count the Number of Properties in a JavaScript Object

Learn how to efficiently calculate the number of properties in a JavaScript object.

To accomplish this, you can use the Object.keys() method. By passing the object you want to inspect as the argument, you can obtain an array that contains all the enumerable (own) properties of the object.

To count the number of properties, simply access the length property of the array generated by Object.keys():

1
2
3
4
5
6
7
const car = {
color: 'Blue',
brand: 'Ford',
model: 'Fiesta'
}

Object.keys(car).length

Note that the term “enumerable properties” refers to properties that have their internal enumerable flag set to true, which is the default behavior. Further information on this topic can be found on the MDN web docs.

tags: [“JavaScript”, “object”, “properties”, “count”]