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():

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.