Generative Data Intelligence

Get Length of JavaScript Object

Date:

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); 
console.log(myString.length); 
console.log(myArray.length); 

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
};



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 pre-defined 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); 

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); 

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); 

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 so 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); 

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: April 20th, 2022

Was this article helpful?

You might also like…

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

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-2022 Stack Abuse. All rights reserved.

spot_img

Latest Intelligence

spot_img

Chat with us

Hi there! How can I help you?