How to Get the Length of an Object in JavaScript



Everything in JavaScript is an Object. You can simply use the Object constructor in JavaScript to create an object or you can define a string object, an array object and add properties and values to it. Sometimes we require to know the length of an object for any specific reason, there is a simplest and quickest way to get the length of a given object in JavaScript is by using the length property of Object.keys() method.

The Object.keys() method returns an array of object property name. The length property is used to get the number of keys present in the object. For example, we have an array with few properties in it.

<script>
    var exObject = {
        Name: 'Item 1',
        Type: 'Type 2',
        Status: 'Closing',
        Date: '2020-02-15'
    }

    console.log(Object.keys(exObject));
    console.log(Object.keys(exObject).length);
</script>

You will get the length value 4 in above example.