All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Length Of Object

Last Updated : Mar 11, 2024

JavaScript Length Of Object

In this tutorial we will show you the solution of JavaScript length of object, as we know array always had more than one key, value pairs so we defined array with some key, value pairs then we needs to pass array to Object.keys() method.

When we appending length property with array it returns count of key, value pairs defined in array as result and we printed on console.

Step By Step Guide On JavaScript Length Of Object :-

Here we defined variable array ‘arr’ with three sets of key, value pairs if you wish to modify array objects then you can change it and for finds length of array objects we needs to append ‘length’ property with that array variables ‘arr’ then returns result stored on variable ‘res’.

Then we printed result on console by console.log() method with array ‘arr’, some string and result variable ‘res’.

<!DOCTYPE html>
<html>
    <head>
        <title>ARRAY OBJECTS LENGTH</title>
    </head>
    <body>
        <script>
            var arr = {
                "Name" : "Pavi",
                "Age" : 23,
                "City" : "Kangeyam",
            }
            var res=Object.keys(arr).length;
            console.log(JSON.stringify(arr));
            console.log(" Array of objects length is "+res);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script we defined variable ‘arr’ with keyword ‘var’ there we initialized three set key, value pairs ["Name" : "Pavi", "Age" : 23, "City" : "Kangeyam”]. Next we need to pass our array ‘arr’ to Object.keys() method because it helps us to holds array keys then using length statement for finds out length of array ‘arr’.
  7. So we appended length with array ‘arr’ and it returns stored on variable ‘res’. For printing associative array we need to use JSON.stringify() method then we can print our array ‘arr’ on console because it helps us to convert it as json data.
  8. At last we printed using console.log() result on console with string ‘Array of objects length is’, variable ‘res’.
  9. In script we know that for print on console we need to use console.log() method and for open console panel right click on window tab select ‘inspect’ then panel will open there you have to click console tab.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to find length of array object in javascript.

When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with results of ‘{"Name":"Pavi","Age":23,"City":"Kangeyam"} Array of objects length is 3’.

If your need to define variable ‘arr’ with some other key, value pairs then you can modify it in this concept accordingly.

I hope this tutorial on JavaScript length of object helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪