Skip to content

How to Get the Length of an Object in Javascript

The Javascript Object is a collection of key value pair.

const obj = {
    key1: value1,
    key2: value2,
    length: 10
};

Object.keys(obj).length // 3 ["key1", "key2", "length"]
obj.length // 10

The Object.keys(obj) returns the array of the keys of the object.

.length is the function available of array.

If you will try to use .length property then object will find length property or key in the object.

Get object length function

Create a object length function.

const obj = {
    key1: value1,
    key2: value2,
    length: 10
};

const len(obj) {
    return Object.keys(obj).length;
};

len(obj); // 3