This post will discuss how to find the length of an object in JavaScript. In other words, find out how many properties or keys an object has.

Objects are composed of key-value pairs, where each key is a string and each value can be any data type. For example, we can create an object like this:

In this object, we have three keys: one, two, and three, and their corresponding numeric values: 1, 2, and 3. Sometimes, we may want to know how many keys or properties an object has, which is also known as the length of an object. However, unlike arrays and strings, objects do not have a built-in length property or function that returns the number of keys they have.

1. Using Object.keys() function

One of the simplest and most widely supported ways to find the length of an object in JavaScript is to use the Object.keys() function. This function takes an object as an argument and returns an array of its own enumerable property names. The enumerable properties are those that can be accessed by a for…in loop. To get the length of this array, we can simply use the length property of arrays, which returns the number of elements in the array. For example:

Download  Run Code

 
This function is simple and easy to use, and works for plain objects and objects created by constructors or classes. However, there are some caveats to be aware of when using this function. It only returns the own enumerable properties of the object, not the inherited ones. For example, if we create a new object that inherits from obj using Object.create(), Object.keys() will not return any keys of obj:

Download  Run Code

 
The Object.keys() function does not include non-enumerable properties, such as built-in functions or symbols. For example, if we add a symbol property to our object, Object.keys() will not include it in the array. To include symbol properties in the array, we can additionally call the Object.getOwnPropertySymbols() function.

Download  Run Code

2. Using Object.values() function

Another way to find the length of an object in JavaScript is to use the Object.values() function. This function takes an object as an argument and returns an array of its own enumerable property values. For example:

Download  Run Code

 
Like Object.keys(), this function works for plain objects and objects created by constructors or classes, and does not include non-enumerable properties, inherited properties, or symbolic properties in the array.

3. Using Object.entries() function

Another way to find the length of an object in JavaScript is to use the Object.entries() function. This function takes an object as an argument and returns an array of its own enumerable property [key, value] pairs. To get the length of this array, we can simply use the length property of arrays. For example:

Download  Run Code

 
This also works for plain objects and objects created by constructors or classes but only returns the own enumerable properties of the object, not the inherited ones, and does not include non-enumerable properties, such as built-in functions or symbols.

4. Using Underscore/Lodash Library

If we prefer using the Underscore or Lodash library, we can use the _.size() function, which returns the collection’s size. This will not include non-enumerable properties, inherited properties, or symbolic properties in the array.

Download Code

That’s all about finding the length of an object in JavaScript.