DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Shallow and Deep Copies in JavaScript: What’s the Difference?
  • Immutability in JavaScript — When and Why Should You Use It
  • An Introduction to Object Mutation in JavaScript
  • Metaprogramming With Proxies and Reflect in JavaScript

Trending

  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • Agentic AI for Automated Application Security and Vulnerability Management
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  1. DZone
  2. Coding
  3. Languages
  4. How to Count the Number of Properties of the JavaScript Object

How to Count the Number of Properties of the JavaScript Object

While working with JavaScript, a dev came across a requirement to count a number of properties in a JavaScript object. Read on to learn how!

By 
Dhananjay Kumar user avatar
Dhananjay Kumar
·
Apr. 11, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
146.7K Views

Join the DZone community and get the full member experience.

Join For Free

While working with JavaScript, I come across a requirement to count a number of properties in a JavaScript object. I found two ways to find the number of properties in an object. They are as follows:

Consider an object, "cat," as demonstrated below:

var cat = {

    name: 'foo',
    age: 9
}

You can find a number of properties by iterating in a for loop and update counter, as shown in the below listing:

let count = 0;
for (var c in cat) {

    count = count + 1;
}
console.log(count);// 2

Above code will print "2" as the output.

The above approach not only prints the object's own enumerable properties, but it also prints properties of objects to which it is linked. To further understand it, let us consider the below listing:

var animal = {

    canRun: true
}

var cat = {

    name: 'foo',
    age: 9
}

cat.__proto__ = animal;

There are two objects, cat andanimal, and the cat object is linked to an animal object using the __proto__ property. Now, when you use a for loop to iterate and count a number of properties, it will also count the enumerable properties of the animal object. Therefore, the code listing below will print "3."

var animal = {

    canRun: true
}

var cat = {

    name: 'foo',
    age: 9
}

cat.__proto__ = animal;

let count = 0;
for (var c in cat) {

    count = count + 1;
}
console.log(count);// 3

A JavaScript for loop will iterate through all the linked properties of the object.

To count the object's own enumerable properties, you may consider using another approach, Object.keys(), which only enumerates the object's own enumerable properties. It does not enumerate the object's linked properties.

Moving forward, let us again consider the cat object which is linked to animal object and count the number of properties using Object.keys:

var animal = {

    canRun: true
}

var cat = {

    name: 'foo',
    age: 9
}

cat.__proto__ = animal;

var count = Object.keys(cat).length;
console.log(count);

Now you will get "2" printed as the output.

Object.keys only enumerates the object's own enumerable properties.

If the object's property enumerable is set to false, then it is not a member of the Object.keys array. Let us again consider the cat object and set its name property enumerable to false.

var cat = {

    name: 'foo',
    age: 9
}

Object.defineProperty(cat, 'name', { enumerable: false });

Now, when you use Object.keys to find a number of properties, it will count only one.

var count = Object.keys(cat).length;
console.log(count);  // print 1

In closing, these are the two ways that you can use to find the number of properties in a JavaScript object.

Object (computer science) Property (programming) JavaScript

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Shallow and Deep Copies in JavaScript: What’s the Difference?
  • Immutability in JavaScript — When and Why Should You Use It
  • An Introduction to Object Mutation in JavaScript
  • Metaprogramming With Proxies and Reflect in JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!