How to Get the Length of a JavaScript Object?

Aug 25, 2022 . Admin



Hello dev,

In this tute, we will discuss javascript get the length of an object example. if you want to see an example of how to get the length of an object in javascript then you are in the right place. if you want to see an example of get the length of an object example using javascript then you are in the right place.

In javascript, we have Object.keys() property, which checks whether there are any properties or not. If we use the length property with Object.keys() then the number of properties will be displayed which is nothing but the length of the object

So, let's see bellow solution:

Example :
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Get the Length of a JavaScript Object? -  MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Get the Length of a JavaScript Object? -  MyWebtuts.com</h1>

        <script type="text/javascript">

            const user = {
                name: 'Pratik Thoriya',
                age: 19,
                country: 'India'
            }

            const length = Object.keys(user).length;

            console.log(`Object length is ${length}`);
        </script>
    </body>
<html>
Output:
Object length is 3
I hope it will help you...
#Javascript