Saturday, October 18, 2025
HomeLanguagesJavascriptHow to get the size of a JavaScript object ?

How to get the size of a JavaScript object ?

In this article, we will see the methods to find the size of a Javascript object. We can get the size of the javascript object with the help of Object.keys() method. 

Syntax:

Object.keys(object_name)
  • object_name

Return Value: an array of all the object’s own enumerable property keys.

Approach:

  • Create an object variable and assign values to it
  • Create a variable that shows the size
  • Call the Object.keys(object-name).length and assign it to the variable (in step 2).

Example: To get the size of a javascript object by Object.keys() method. 

javascript




let object1= { "rajnish":"singh",
               "sanjeev":"sharma",
               "suraj":"agrahari",
               "yash":"khandelwal"
             };
let count= Object.keys(object1).length;
console.log(count);


Output:

4

Approach 2: Another way to get the size of the javascript object is:

  • Create an object variable and assign values to it
  • Create a variable that shows the size
  • Pass the object name to Object.objsize which further enters the function which calculates and returns the size and assigns it to the variable.

Example:

javascript




Object.objsize = function(obj) {
    let size = 0, key;
 
    for (key in obj) {
        if (obj.hasOwnProperty(key))
        size++;
    }
    return size;
};
 
let object1 = { "rajnish":"singh",
                "sanjeev":"sharma",
                "suraj":"agrahari",
                "yash":"khandelwal"
              };
 
let count = Object.objsize(object1);
console.log(count);


Output:

4
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS