Get Length of JavaScript Object

Introduction

Objects are used to store a set of properties, each of which can be thought of as a link between a name (or key) and a value (a collection of key-value pairs).

In this guide, we will learn how to get the length of a JavaScript object.

Checking the length of an Object is not a common and basic operation; however, it is critical to understand how this can be accomplished and to avoid some unnecessary bugs. The object does not have a length property by default. The length property is only available to arrays and strings.

For example:

let myObject = {firstName: "John", lastName: "Doe"};
let myString = 'John Doe';
let myArray = [71, 32, 78, 54];

console.log(myObject.length); // undefined
console.log(myString.length); // 8
console.log(myArray.length); // 4

There are basically two ways to get the length of an object in JavaScript: using any of the object static methods or the for...in loop method. Let's start by creating an object, either with the object literal syntax or using the new keyword:

let subjectScores = {
    chemistry: 40,
    mathematics: 70,
    physics: 90,
    english: 68,
    biology: 77
};

//Or

let subjectScores = new Object();

subjectScores["chemistry"] = 40;
subjectScores["mathematics"] = 70;
subjectScores["physics"] = 90;
subjectScores["english"] = 68;
subjectScores["biology"] = 77;

Get Length of Object With Object Static Methods

Static methods are predefined methods that we can access on any object. To determine the length of an object, we can use object static methods such as Object.keys(), Object.values(), and Object.entries(). These methods return either the key, values, or key-value pairs as arrays, allowing us to use the length property to determine the object's length.

Note: Properties are key-value pairs that define an object. Each property in an object is given a name (also known as a key) and a value (also known as a value). Depending on which properties you want to enumerate, you can extract keys() and values() separately, or entries(), which are key-value pairs.

Get Length of Object With Object.keys()

The Object.keys() method returns an array of properties of the Object, we will then make use of the length property to get the number of elements in the array (length of the object). For example, making use of the object we created at the beginning of this article:

let objectLength = Object.keys(subjectScores).length; 
console.log(objectLength); // 5

Get Length of Object With Object.values()

The Object.values() method returns an array which contains the values of the Object. We will also make use of the length property to get the number of elements. For example, making use of the object we created at the beginning of this article:

let objectLength = Object.values(subjectScores).length; 
console.log(objectLength); // 5

Get Length of Object With Object.entries()

The Object.entries() method returns an array of the key-value pair of an Object. We can use the length property to get the number of elements. For example, making use of the object we created at the beginning of this article:

let objectLength = Object.entries(subjectScores).length; 
console.log(objectLength); // 5

Get Length of Object Using for…in Loop

The for…in loop is used to iterate the properties of the object. To get the length, all we would do is to create a variable and increase the counter as long as the loop continues.

let objectLength = 0;

for (let key in subjectScores) {
    objectLength++;
}

console.log(objectLength); // 5

Conclusion

In this article, we have learned how to get the length of an object via either static methods or looping through via the for…in method.

Last Updated: November 8th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Joel OlawanleAuthor

Frontend Developer & Technical Writer

Project

React State Management with Redux and Redux-Toolkit

# javascript# React

Coordinating state and keeping components in sync can be tricky. If components rely on the same data but do not communicate with each other when...

David Landup
Uchechukwu Azubuko
Details

Getting Started with AWS in Node.js

Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms