Categories
JavaScript Answers

How to get the number of elements in a JavaScript object?

Spread the love

Sometimes, we want to get the number of elements in a JavaScript object.

In this article, we’ll look at how to get the number of elements in a JavaScript object.

How to get the number of elements in a JavaScript object?

To get the number of elements in a JavaScript object, we can use the Object.keys method.

For instance, we write

const count = Object.keys(obj).length;

to call Object.keys with obj to get an array with the key strings in obj.

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

Conclusion

To get the number of elements in a JavaScript object, we can use the Object.keys method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *