Get the Length of an Object in JavaScript (5 ways)

A widespread requirement many people encounter while working with javascript objects is to get the size of an object. This article demonstrates easy ways to get the length of a javascript object using different methods and various example illustrations.

Table of Contents:

Get the Length of an Object using Object.keys()

Javascript’s Object.keys() returns an array of enumerable property names of the object. The method iterates the keys in the same order as that of a standard loop.

Example:-

Get the length of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’ }

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var lengthOfObject = Object.keys(personObject).length;
console.log(lengthOfObject);

Output:-

Length of the Object is: 4

Get the Length of an Object using Object.getOwnPropertyNames()

Javascript’s Object.getOwnPropertyNames() returns an array of all properties (except for those which use Symbol) enumerable as well as non-enumerable of the given object.

Example:-

Get the length of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’ }

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var lengthOfObject = Object.getOwnPropertyNames(personObject).length;
console.log("Length of the Object is: " + lengthOfObject);

Output:-

Length of the Object is: 4

Get the Length of an Object using Object.values()

Javascript’s Object.values() returns an array of enumerable property values of the object.

Example:-

Get the length of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’ }

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var lengthOfObject = Object.values(personObject).length;
console.log("Length of the Object is: " + lengthOfObject);

Output:-

Length of the Object is: 4

Get the Length of an Object Excluding the Properties in Property Chain

Javascript’s hasOwnProperty() method determines if the particular property is its own property or not (as opposed to inheriting it). The returned value is boolean.

The example below will get the object’s length only for its properties and not the inherited ones.

Example:-

Get the length of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’ }

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var lengthOfObject = 0;
for(var l in personObject)
{
    if(personObject.hasOwnProperty(l))
    lengthOfObject++;
}
console.log("Length of the Object is: " + lengthOfObject);

Output:-

Length of the Object is: 4

Get the Length of an Object using Object.entries()

Javascript’s Object.entries() returns an array of enumerable key-value pairs of the object.

Example:-

Get the length of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’ }

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var lengthOfObject = Object.entries(personObject).length;
console.log("Length of the Object is: " + lengthOfObject);

Output:-

Length of the Object is: 4

I hope this article helped you to get the length of an object in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top