How to count a JavaScript object’s attributes?

Sometimes, we want to count a JavaScript object’s attributes.

In this article, we’ll look at how to count a JavaScript object’s attributes.

How to count a JavaScript object’s attributes?

To count a JavaScript object’s attributes, we can use the Object.keys method.

For instance, we write

Object.keys(myObject).length;

to call Object.keys with myObject to get an array of non-inherited property keys in myObject.

Then we use the length property to get the number of properties in myObject.

Conclusion

To count a JavaScript object’s attributes, we can use the Object.keys method.