# How to get the length of an object in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 14, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-object-length

> A short guide to learning how to use the Object.keys() method to get the length of a JavaScript object.

To get the length of an object in JavaScript:

1. Pass the object to the `Object.keys()` method as a parameter.
2. The [`Object.keys()`](https://attacomsian.com/blog/javascript-iterate-objects#objectkeys-method) method returns an array of a given object's own enumerable property names.
3. Use the `length` property to get the number of elements in that array.

Here is an example:

```javascript
const user = {
  name: 'John Doe',
  age: 23,
  country: 'United States'
}

const length = Object.keys(user).length

console.log(`Object length is ${length}`)
// Object length is 3
```

In the above code, we used the `Object.keys()` method to get an array of the object's keys. The `length` property of the keys array returns the count of key-value pairs in the object.
