How to Get the Length of Object in JavaScript
- Using Object.keys()
- Using Object.entries()
- Using a for…in Loop
- Using JSON.stringify()
- Conclusion
- FAQ

When working with JavaScript, you may often need to determine the size of an object. Knowing how many properties an object has can be crucial for various operations, such as validating data, managing state, or even optimizing performance.
In this article, we will explore different methods to get the length of an object in JavaScript. From using built-in methods to employing modern techniques, we will cover everything you need to know. Whether you’re a beginner or an experienced developer, understanding these methods will enhance your coding skills and improve your ability to manipulate objects effectively.
Using Object.keys()
One of the simplest and most effective ways to get the length of an object in JavaScript is by utilizing the Object.keys()
method. This method returns an array of a given object’s own enumerable property names. By determining the length of this array, you can easily find out how many properties exist in the object.
Here’s how you can do it:
const person = {
name: 'John',
age: 30,
city: 'New York'
};
const lengthOfObject = Object.keys(person).length;
console.log(lengthOfObject);
Output:
3
In this example, we have an object named person
with three properties: name
, age
, and city
. By calling Object.keys(person)
, we get an array containing these property names: ['name', 'age', 'city']
. The length
property of this array gives us the total number of properties, which in this case is 3. This method is straightforward and works well for most scenarios, especially when you only need to count the object’s own properties.
Using Object.entries()
Another efficient way to determine the length of an object is by using the Object.entries()
method. Similar to Object.keys()
, this method returns an array, but instead of just the keys, it returns an array of the object’s own enumerable string-keyed property [key, value] pairs. By checking the length of this array, you can quickly ascertain how many properties the object has.
Here’s an example:
const car = {
make: 'Toyota',
model: 'Camry',
year: 2022
};
const lengthOfCarObject = Object.entries(car).length;
console.log(lengthOfCarObject);
Output:
3
In this case, we have an object called car
with three properties: make
, model
, and year
. By using Object.entries(car)
, we get an array of key-value pairs: [['make', 'Toyota'], ['model', 'Camry'], ['year', 2022]]
. The length of this array is also 3, indicating that there are three properties in the car
object. This method is particularly useful if you also want to access the values along with the keys.
Using a for…in Loop
If you prefer a more traditional approach, you can use a for...in
loop to iterate over the properties of an object and count them manually. This method is less elegant than the previous ones but can be useful in certain scenarios, especially if you need to perform additional operations on each property.
Here’s how you can implement this:
const book = {
title: '1984',
author: 'George Orwell',
pages: 328
};
let count = 0;
for (let key in book) {
if (book.hasOwnProperty(key)) {
count++;
}
}
console.log(count);
Output:
3
In this example, we have an object named book
with three properties: title
, author
, and pages
. We initialize a counter variable count
to zero. The for...in
loop iterates over each property in the book
object. The hasOwnProperty()
method ensures that we only count the object’s own properties and not any inherited ones. After the loop completes, we log the count, which is 3 in this case. While this method works, it is generally less efficient and more verbose than the previous methods.
Using JSON.stringify()
An unconventional but interesting way to get the length of an object is by converting it to a JSON string and then counting the number of keys. This method is not typically recommended due to its inefficiency, but it can be useful in specific situations where you might need to serialize the object.
Here’s an example:
const user = {
username: 'alice',
email: 'alice@example.com',
isActive: true
};
const lengthOfUserObject = Object.keys(JSON.parse(JSON.stringify(user))).length;
console.log(lengthOfUserObject);
Output:
3
In this example, we have a user
object with three properties: username
, email
, and isActive
. We first convert the object to a JSON string using JSON.stringify()
, then parse it back to an object with JSON.parse()
. Finally, we use Object.keys()
to count the number of keys. The length is 3, as expected. While this method works, it is generally slower and more complex than the other options, so use it judiciously.
Conclusion
In summary, knowing how to get the length of an object in JavaScript is an essential skill for any developer. Whether you choose to use Object.keys()
, Object.entries()
, a for...in
loop, or even the unconventional JSON method, each approach has its advantages and specific use cases. By mastering these techniques, you’ll be better equipped to handle objects in your JavaScript applications effectively. So, the next time you need to count properties, you can confidently choose the method that best fits your needs.
FAQ
-
How can I check if an object is empty in JavaScript?
You can check if an object is empty by usingObject.keys(obj).length === 0
. If the length is zero, the object has no properties. -
Can I count nested properties in an object?
The methods discussed only count the top-level properties. To count nested properties, you would need to implement a recursive function that traverses the object. -
Are there performance differences between these methods?
Yes, methods likeObject.keys()
andObject.entries()
are generally faster than using afor...in
loop, especially for large objects. However, the performance difference may not be significant for smaller objects.
-
What about inherited properties?
The methods discussed only count the object’s own properties, not inherited ones. If you want to include inherited properties, you can omit thehasOwnProperty()
check in thefor...in
loop. -
Is it possible to count properties of an object with symbols?
Yes, you can useObject.getOwnPropertySymbols(obj).length
to count properties defined as symbols. However, this requires a separate call since symbols are not included in the standard property count.